Include a header file required for build on mac 10.4
[supercollider.git] / Help / Scheduling / Semaphore.html
blob463a28bc64d839d0d913d7311238aafd09fdc102
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #000000}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #000000; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000; min-height: 12.0px}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
17 span.s1 {font: 18.0px Helvetica}
18 span.s2 {font: 9.0px Monaco}
19 span.s3 {color: #000000}
20 span.s4 {color: #bf0000}
21 span.s5 {color: #0000bf}
22 span.s6 {color: #606060}
23 span.Apple-tab-span {white-space:pre}
24 </style>
25 </head>
26 <body>
27 <p class="p1"><span class="s1"><b>Semaphore</b></span><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><b>control parallel execution of threads</b></p>
28 <p class="p2"><br></p>
29 <p class="p2"><br></p>
30 <p class="p1">superclass: Object</p>
31 <p class="p2"><br></p>
32 <p class="p2"><br></p>
33 <p class="p1"><b><span class="Apple-tab-span"> </span>*new(count)<span class="Apple-tab-span"> </span></b>create a new instance, set the maximum number of running threads (default: 1).</p>
34 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
35 <p class="p1"><b><span class="Apple-tab-span"> </span>count<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>determines the number of running threads</p>
36 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
37 <p class="p1"><b><span class="Apple-tab-span"> </span>clear<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>remove any reference to threads, but do not rescedule any pending ones.</p>
38 <p class="p2"><span class="Apple-tab-span"> </span></p>
39 <p class="p1"><b><span class="Apple-tab-span"> </span>wait<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>stop current thread if already too many are running, otherwise continue.</p>
40 <p class="p2"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b></p>
41 <p class="p1"><b><span class="Apple-tab-span"> </span>signal<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>unblock the semaphore, reschedule next pending thread.</p>
42 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
43 <p class="p3"><br></p>
44 <p class="p3"><br></p>
45 <p class="p3"><br></p>
46 <p class="p4">// example</p>
47 <p class="p3"><br></p>
48 <p class="p3"><br></p>
49 <p class="p4">// allow only one thread</p>
50 <p class="p5">(</p>
51 <p class="p6"><span class="s3">c = </span>Semaphore<span class="s3">(1);</span></p>
52 <p class="p5">fork {<span class="Apple-converted-space"> </span></p>
53 <p class="p5"><span class="Apple-tab-span"> </span>c.wait;<span class="Apple-converted-space"> </span></p>
54 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 1&gt; now I am doing something for 10 seconds. Block the semaphore meanwhile."<span class="s3">.postln;<span class="Apple-converted-space"> </span></span></p>
55 <p class="p5"><span class="Apple-tab-span"> </span>10.wait;</p>
56 <p class="p5"><span class="Apple-tab-span"> </span>c.signal;</p>
57 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 1&gt; ok, done. Release the semaphore."<span class="s3">.postln;<span class="Apple-converted-space"> </span></span></p>
58 <p class="p5">};</p>
59 <p class="p5">fork {</p>
60 <p class="p5"><span class="Apple-tab-span"> </span>3.0.rand.wait;</p>
61 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 2&gt; I would like to go on, if I may."<span class="s3">.postln;</span></p>
62 <p class="p5"><span class="Apple-tab-span"> </span>c.wait; <span class="s4">// may I?</span></p>
63 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 2&gt; this took until the other thread has released the semaphore. "</p>
64 <p class="p7"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>"Blocking for 4 seconds."<span class="s3">.postln;</span></p>
65 <p class="p5"><span class="Apple-tab-span"> </span>4.wait;</p>
66 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 2&gt; ok, done. Releasing the semaphore"<span class="s3">.postln;</span></p>
67 <p class="p5"><span class="Apple-tab-span"> </span>c.signal;</p>
68 <p class="p5">};</p>
69 <p class="p5">fork {</p>
70 <p class="p5"><span class="Apple-tab-span"> </span>4.wait;</p>
71 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 3&gt; I, too, would like to go on, if I may."<span class="s3">.postln;</span></p>
72 <p class="p5"><span class="Apple-tab-span"> </span>c.wait; <span class="s4">// may I?</span></p>
73 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 3&gt; this took until both other threads had released the semaphore."<span class="s3">.postln;</span></p>
74 <p class="p5"><span class="Apple-tab-span"> </span>c.signal;</p>
75 <p class="p5">};</p>
76 <p class="p5">)</p>
77 <p class="p3"><br></p>
78 <p class="p3"><br></p>
79 <p class="p3"><br></p>
80 <p class="p4">// allow two threads at a time.</p>
81 <p class="p5">(</p>
82 <p class="p6"><span class="s3">c = </span>Semaphore<span class="s3">(2);</span></p>
83 <p class="p5">fork {<span class="Apple-converted-space"> </span></p>
84 <p class="p5"><span class="Apple-tab-span"> </span>c.wait;<span class="Apple-converted-space"> </span></p>
85 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 1&gt; now I am doing something for 20 seconds. Block the semaphore."<span class="s3">.postln;<span class="Apple-converted-space"> </span></span></p>
86 <p class="p5"><span class="Apple-tab-span"> </span>10.wait;</p>
87 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 1&gt; ok, done. Releasing the semaphore"<span class="s3">.postln;</span></p>
88 <p class="p5"><span class="Apple-tab-span"> </span>c.signal;</p>
89 <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-converted-space"> </span></p>
90 <p class="p5">};</p>
91 <p class="p5">fork {</p>
92 <p class="p5"><span class="Apple-tab-span"> </span>rrand(3.0, 5.0).wait;</p>
93 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 2&gt; I would like to go on, if I may."<span class="s3">.postln;</span></p>
94 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span>if(c.count &lt;= 0) { </span>"thread 3&gt; ok, then I wait ..."<span class="s3">.postln };</span></p>
95 <p class="p5"><span class="Apple-tab-span"> </span>c.wait; <span class="s4">// may I?</span></p>
96 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 1&gt; ok, going ahead."<span class="s3">.postln;</span></p>
97 <p class="p5"><span class="Apple-tab-span"> </span>17.wait;</p>
98 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 2&gt; ok, done. Releasing the semaphore"<span class="s3">.postln;</span></p>
99 <p class="p5"><span class="Apple-tab-span"> </span>c.signal;</p>
100 <p class="p5">};</p>
101 <p class="p5">fork {</p>
102 <p class="p5"><span class="Apple-tab-span"> </span>6.wait;</p>
103 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 3&gt; I, too, would like to go on, if I may."<span class="s3">.postln;</span></p>
104 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span>if(c.count &lt;= 0) { </span>"thread 3&gt; ok, then I wait ..."<span class="s3">.postln };</span></p>
105 <p class="p5"><span class="Apple-tab-span"> </span>c.wait; <span class="s4">// may I?</span></p>
106 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 3&gt; ok, this took until the first thread had released the semaphore. "</p>
107 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>"Ok, doing something for 4 seconds. Block the semaphore"<span class="s3">.postln;</span></p>
108 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>4.wait;</p>
109 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"Releasing the semaphore."<span class="s3">.postln;</span></p>
110 <p class="p5"><span class="Apple-tab-span"> </span>c.signal;</p>
111 <p class="p3"><span class="Apple-tab-span"> </span></p>
112 <p class="p5">};</p>
113 <p class="p5">fork {</p>
114 <p class="p5"><span class="Apple-tab-span"> </span>7.wait;</p>
115 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 4&gt; Me, the fourth one, would like to go on, if I may."<span class="s3">.postln;</span></p>
116 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span>if(c.count &lt;= 0) { </span>"thread 4&gt; ok, then I wait ..."<span class="s3">.postln };</span></p>
117 <p class="p5"><span class="Apple-tab-span"> </span>c.wait; <span class="s4">// may I?</span></p>
118 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"thread 4&gt; ok, this took until the third thread had released the semaphore. "</p>
119 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>"Ok, doing something for 3 seconds. Block the semaphore"<span class="s3">.postln;</span></p>
120 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>3.wait;</p>
121 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span>"Releasing the semaphore."<span class="s3">.postln;</span></p>
122 <p class="p5"><span class="Apple-tab-span"> </span>c.signal;</p>
123 <p class="p5">};</p>
124 <p class="p3"><br></p>
125 <p class="p5">)</p>
126 <p class="p3"><br></p>
127 <p class="p3"><br></p>
128 <p class="p4">// grant exclusive access to data to only one thread</p>
129 <p class="p4">// there should never be mixed values in the data array</p>
130 <p class="p3"><br></p>
131 <p class="p5">(</p>
132 <p class="p5"><span class="s5">var</span> data, useAndModify;</p>
133 <p class="p3"><br></p>
134 <p class="p5">data = [1, 2, 3];</p>
135 <p class="p6"><span class="s3">c = </span>Semaphore<span class="s3">(1);</span></p>
136 <p class="p4">// c = Semaphore(2); use this to test how it would behave without exclusive access.</p>
137 <p class="p5">useAndModify = { <span class="s5">|newData, who|</span></p>
138 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span>postln(who + </span>"trying to get blocking access."<span class="s3">);</span></p>
139 <p class="p5"><span class="Apple-tab-span"> </span>if(c.count &lt;= 0) { who + <span class="s6">"ok, then I wait ..."</span>.postln };</p>
140 <p class="p4"><span class="s3"><span class="Apple-tab-span"> </span>c.wait; </span>// may I access? if not, I wait. if yes, disallow others.</p>
141 <p class="p5"><span class="Apple-tab-span"> </span><span class="s6">"\n"</span>.post;</p>
142 <p class="p5"><span class="Apple-tab-span"> </span>(who + <span class="s6">"continuing..."</span>).postln;</p>
143 <p class="p5"><span class="Apple-tab-span"> </span>data.do({ <span class="s5">|x|</span></p>
144 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.1.wait;</p>
145 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>postln(who + x);</p>
146 <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
147 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>});</p>
148 <p class="p5"><span class="Apple-tab-span"> </span><span class="s6">"\n"</span>.post;</p>
149 <p class="p5"><span class="Apple-tab-span"> </span>newData.do { <span class="s5">|x, i|</span> data[i] = x };</p>
150 <p class="p5"><span class="Apple-tab-span"> </span>postln(who + <span class="s6">"rewriting data to:"</span> + newData);</p>
151 <p class="p5"><span class="Apple-tab-span"> </span>postln(who + <span class="s6">"releasing"</span>);</p>
152 <p class="p4"><span class="s3"><span class="Apple-tab-span"> </span>c.signal; </span>// allow others access again</p>
153 <p class="p5">};</p>
154 <p class="p3"><br></p>
155 <p class="p4">// e.g. set the values to integers</p>
156 <p class="p6"><span class="s3">u = </span>Routine<span class="s3"> {</span></p>
157 <p class="p5"><span class="Apple-tab-span"> </span><span class="s5">inf</span>.do { <span class="s5">|i|</span></p>
158 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>useAndModify.value([100, 200, 300], <span class="s6">"thread 1&gt;"</span>);</p>
159 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>rrand(1, 3).wait;</p>
160 <p class="p5"><span class="Apple-tab-span"> </span>}</p>
161 <p class="p5">};</p>
162 <p class="p3"><br></p>
163 <p class="p4">// e.g. set the values to floats</p>
164 <p class="p5">k =<span class="Apple-converted-space">  </span><span class="s5">Routine</span> {</p>
165 <p class="p5"><span class="Apple-tab-span"> </span>0.5.wait;</p>
166 <p class="p5"><span class="Apple-tab-span"> </span><span class="s5">inf</span>.do { <span class="s5">|i|</span></p>
167 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>useAndModify.value([pi, 0.5pi, 2pi], <span class="s6">"thread 2&gt;"</span>);</p>
168 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>rrand(1, 5).wait;</p>
169 <p class="p5"><span class="Apple-tab-span"> </span>}</p>
170 <p class="p5">};</p>
171 <p class="p5">u.play;</p>
172 <p class="p5">k.play;</p>
173 <p class="p5">);</p>
174 </body>
175 </html>