polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Scheduling / Condition.html
blob2897bf3d3efa0eba5ed5c381c85356e60323b2b2
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="824.42">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #a71e12}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #a71e12; min-height: 12.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0019b7}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
18 span.s1 {font: 18.0px Helvetica}
19 span.s2 {font: 9.0px Monaco}
20 span.s3 {color: #000000}
21 span.s4 {color: #0019b7}
22 span.s5 {color: #606060}
23 span.Apple-tab-span {white-space:pre}
24 </style>
25 </head>
26 <body>
27 <p class="p1"><span class="s1"><b>Condition</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>block execution of a thread</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(test)<span class="Apple-tab-span"> </span></b>create a new instance, set the <b>test</b> variable.</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>test<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>return the test variable (boolean)</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>wait<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>wait until the condition is true and signalled</p>
38 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
39 <p class="p1"><b><span class="Apple-tab-span"> </span>hang(val)<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>wait for <b>val</b> time, regardless of test</p>
40 <p class="p2"><b><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>if <b>test</b> is true, reschedule blocked threads</p>
42 <p class="p2"><b><span class="Apple-tab-span"> </span></b></p>
43 <p class="p1"><b><span class="Apple-tab-span"> </span>unhang<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>resume threads</p>
44 <p class="p3"><br></p>
45 <p class="p3"><br></p>
46 <p class="p3"><br></p>
47 <p class="p3"><br></p>
48 <p class="p4">// example</p>
49 <p class="p5"><br></p>
50 <p class="p3"><br></p>
51 <p class="p6">(</p>
52 <p class="p7"><span class="s3">c = </span>Condition<span class="s3">.new(</span>false<span class="s3">);</span></p>
53 <p class="p3"><br></p>
54 <p class="p7">Routine<span class="s3"> {</span></p>
55 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
56 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waited for 1 second"<span class="s3">.postln;</span></p>
57 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
58 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waited for another second, now waiting for you ... "<span class="s3">.postln;</span></p>
59 <p class="p6"><span class="Apple-tab-span"> </span>c.wait;</p>
60 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"the condition has stopped waiting."<span class="s3">.postln;</span></p>
61 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
62 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waited for another second"<span class="s3">.postln;</span></p>
63 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waiting for you ... "<span class="s3">.postln;</span></p>
64 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>c.test = <span class="s4">false</span>;</p>
65 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>c.wait;</p>
66 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"the condition has stopped waiting."<span class="s3">.postln;</span></p>
67 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
68 <p class="p6"><span class="Apple-tab-span"> </span><span class="s5">"the end"</span>.postln;</p>
69 <p class="p6">}.play;<span class="Apple-converted-space"> </span></p>
70 <p class="p6">)</p>
71 <p class="p3"><br></p>
72 <p class="p4">// continue</p>
73 <p class="p6">(</p>
74 <p class="p6">c.test = <span class="s4">true</span>;</p>
75 <p class="p6">c.signal;</p>
76 <p class="p6">)</p>
77 <p class="p3"><br></p>
78 <p class="p4">// a typical use is a routine that can pause under certin conditions:</p>
79 <p class="p6">(</p>
80 <p class="p6">c = <span class="s4">Condition</span>.new;</p>
81 <p class="p6">fork { loop { 1.wait; <span class="s5">"going"</span>.postln; c.wait } };</p>
82 <p class="p6">)</p>
83 <p class="p6">c.test = <span class="s4">true</span>; c.signal;</p>
84 <p class="p6">c.test = <span class="s4">false</span>;</p>
85 <p class="p3"><br></p>
86 <p class="p3"><br></p>
87 <p class="p3"><br></p>
88 <p class="p4">// the same, using hang</p>
89 <p class="p3"><br></p>
90 <p class="p6">(</p>
91 <p class="p6">c = <span class="s4">Condition</span>.new;</p>
92 <p class="p3"><br></p>
93 <p class="p7">Routine<span class="s3"> {</span></p>
94 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
95 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waited for 1 second"<span class="s3">.postln;</span></p>
96 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
97 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waited for another second, now waiting for you ... "<span class="s3">.postln;</span></p>
98 <p class="p6"><span class="Apple-tab-span"> </span>c.hang;</p>
99 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"the condition has stopped waiting."<span class="s3">.postln;</span></p>
100 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
101 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waited for another second"<span class="s3">.postln;</span></p>
102 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"waiting for you ... "<span class="s3">.postln;</span></p>
103 <p class="p6"><span class="Apple-tab-span"> </span>c.hang;</p>
104 <p class="p8"><span class="s3"><span class="Apple-tab-span"> </span></span>"the condition has stopped waiting."<span class="s3">.postln;</span></p>
105 <p class="p6">}.play;<span class="Apple-converted-space"> </span></p>
106 <p class="p6">)</p>
107 <p class="p3"><br></p>
108 <p class="p4">// continue</p>
109 <p class="p6">c.unhang;</p>
110 <p class="p3"><br></p>
111 <p class="p3"><br></p>
112 </body>
113 </html>