File: SingleAssignment.html

package info (click to toggle)
polyml 5.6-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 31,892 kB
  • ctags: 34,453
  • sloc: cpp: 44,983; ansic: 24,520; asm: 14,850; sh: 11,730; makefile: 551; exp: 484; python: 253; awk: 91; sed: 9
file content (98 lines) | stat: -rw-r--r-- 5,100 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<TITLE>The SingleAssignment structure</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="docstyle.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY BGCOLOR="#ffffff">
<ul class="nav">
	<li><a href="Signal.html">Previous</a></li>
	<li><a href="Basis.html">Up</a></li>
	<li><a href="Threads.html">Next</a></li>
</ul>
<H2><STRONG><font face="Arial, Helvetica, sans-serif">SingleAssignment structure</font></STRONG></H2>
<p>The <span class="identifier">SingleAssignment</span> structure provides a reference 
  that can be assigned a value only once.</p>
<pre class="mainsig">structure SingleAssignment:
  sig
    type 'a saref
    val saref: unit -&gt; 'a saref

    exception Locked
    val saset: 'a saref * 'a -&gt; unit
    val savalue: 'a saref -&gt; 'a option
  end</pre>
<div class="entryblock"> 
  <pre class="entrycode"><a name="saref_type" id="saref_type"></a>type 'a saref</pre>
  <div class="entrytext">
    <p>The type of a single-assignment reference. It is similar to the standard 
      <span class="identifier">ref</span> type constructor.</p>
  </div>
</div>
<div class="entryblock"> 
  <pre class="entrycode"><a name="saref"></a>val saref: unit -&gt; 'a saref</pre>
  <div class="entrytext">
    <p>Construct a single-assignment reference.</p>
  </div>
</div>
<div class="entryblock"> 
  <pre class="entrycode"><a name="Locked"></a>exception Locked</pre>
  <div class="entrytext">
    <p>This exception is raised if an attempt is made to assign a value twice 
      to the same reference.</p>
  </div>
</div>
<div class="entryblock"> 
  <pre class="entrycode"><a name="saset" id="saset"></a>val saset: 'a saref * 'a -&gt; unit</pre>
  <div class="entrytext">
    <p>Assign a value to the reference. If it has already been assigned a value 
      this will raise Locked. Note that this function is not thread-safe. A <a href="Threads.html#mutex_type"><span class="identifier">mutex</span></a> 
      must be associated with reference if there is the possibility that two different 
      threads may attempt to assign to the same reference.</p>
  </div>
</div>
<div class="entryblock"> 
  <pre class="entrycode"><a name="savalue" id="savalue"></a>val savalue: 'a saref -&gt; 'a option</pre>
  <div class="entrytext">
    <p>Extract the current value of the reference. If it has not yet been assigned 
      a value it will return <span class="identifier">NONE</span>. If it has, 
      it will return <span class="identifier">SOME v</span> where <span class="identifier">v</span> 
      is the value that was assigned.</p>
  </div>
</div>
<p>The reason behind the <span class="identifier">SingleAssignment</span> structure 
  has to do with the way the Poly/ML storage management system deals with <em>mutable</em> 
  and <em>immutable</em> data. Immutable memory cells are given a value when they 
  are created and once created never change. They are used for lists, tuples, 
  vectors and other datatypes. In contrast, refs and arrays are mutable data. 
  They are given a value when they are created in the same way as immutable data 
  but their contents can change by assignment. In addition Standard ML also distinguishes 
  between mutable and immutable data in the treatment of equality. Immutable data 
  structures are considered equal if their contents are the same, mutable cells 
  are considered equal only if they are the pointers to the same cell.</p>
<p>Because of these differences mutable data has to be handled separately from 
  immutable data by the garbage collector. Using mutable cells imposes an extra 
  cost on each collection when compared with immutable data. In addition it is 
  possible to reduce the heap size by merging immutable cells that have the same 
  contents. In some circumstances the garbage collector may do this automatically; 
  more often it is done explicitly using <a href="PolyMLStructure.html#shareCommonData"><span class="identifier">PolyML.shareCommonData</span></a>. 
</p>
<p>The <span class="identifier">SingleAssignment</span> structure allows for a 
  combination of mutable and immutable data. A value of type <span class="identifier">saref</span> 
  is initially mutable but once it has been assigned a value it is marked as immutable. 
  This allows the garbage-collector and sharing code to treat it as purely immutable 
  once it has been locked.</p>
<p>A typical use for a single-assignment reference is when a data structure is 
  being built by multiple threads. A <span class="identifier">saref</span> can 
  be used within the data structure to represent a portion of the structure to 
  be built and a thread created to build it. When the thread completes it assigns 
  the <span class="identifier">saref</span> with the results of its work. The 
  full structure is now immutable with all the advantages of immutable data.</p>
<ul class="nav">
	<li><a href="Signal.html">Previous</a></li>
	<li><a href="Basis.html">Up</a></li>
	<li><a href="Threads.html">Next</a></li>
</ul>
</BODY>
</HTML>