rsc3/doc-schelp/HelpSource/Classes/Condition.schelp

122 lines
2.4 KiB
Text
Raw Permalink Normal View History

2022-08-24 13:53:18 +00:00
CLASS::Condition
categories::Scheduling
summary::Block the execution of a thread
CLASSMETHODS::
method::new
Create a new instance, set the strong::test:: variable.
INSTANCEMETHODS::
method::test
Answer whether the condition will block or not (boolean).
method::wait
Wait until the condition is true and signalled. This only works in a Routine. This method yields a symbol (\hang), so that the clock doesn't reschedule the Routine.
code::
c = Condition(false); fork { 0.5.wait; "started ...".postln; c.wait; "... and finished.".postln };
c.test = true;
c.signal;
::
method::hang
Wait for strong::value:: time, regardless of test. This only works in a Routine. This method yields a symbol (\hang), so that the clock doesn't reschedule the Routine.
code::
c = Condition.new; fork { 0.5.wait; "started ...".postln; c.hang; "... and finished.".postln };
c.unhang;
::
method::signal
If link::#-test:: is true, reschedule blocked threads.
method::unhang
Resume threads.
EXAMPLES::
code::
(
c = Condition.new(false);
Routine {
1.wait;
"waited for 1 second".postln;
1.wait;
"waited for another second, now waiting for you ... ".postln;
c.wait;
"the condition has stopped waiting.".postln;
1.wait;
"waited for another second".postln;
"waiting for you ... ".postln;
c.test = false;
c.wait;
"the condition has stopped waiting.".postln;
1.wait;
"the end".postln;
}.play;
)
// continue
(
c.test = true;
c.signal;
)
// a typical use is a routine that can pause under certain conditions:
(
c = Condition.new;
fork { loop { 1.wait; "going".postln; c.wait } };
)
c.test = true; c.signal;
c.test = false;
::
code::
// the same, using hang
(
c = Condition.new;
Routine {
1.wait;
"waited for 1 second".postln;
1.wait;
"waited for another second, now waiting for you ... ".postln;
c.hang;
"the condition has stopped waiting.".postln;
1.wait;
"waited for another second".postln;
"waiting for you ... ".postln;
c.hang;
"the condition has stopped waiting.".postln;
}.play;
)
// continue
c.unhang;
::
Waiting for Synths to end (waitForFree) uses a Condition implicitly:
code::
(
SynthDef(\help, {
var mod = LFNoise2.kr(ExpRand(0.5, 2)) * 0.5;
var snd = mod * Blip.ar(Rand(200, 800) * (mod + 1));
Out.ar(0, snd);
FreeSelf.kr(mod < 0); // free the synth when amplitude goes below 0.
}).add;
)
(
fork {
10.do {
"started a synth".postln;
Synth(\help).waitForFree;
"This one ended. Wait a second, I will start the next one.".postln;
1.wait;
};
"This is it.".postln;
}
);
::