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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
CLASS::Semaphore
categories::Scheduling
summary::control parallel execution of threads
CLASSMETHODS::
method::new
Create a new instance, set the maximum number of running threads (default: 1).
INSTANCEMETHODS::
method::count
Determines the number of running threads.
method::clear
Remove any reference to threads, but do not reschedule any pending ones.
method::wait
Stop current thread if already too many are running, otherwise continue.
method::signal
Unblock the semaphore, reschedule next pending thread.
EXAMPLES::
code::
// allow only one thread
(
c = Semaphore(1);
fork {
c.wait;
"thread 1> now I am doing something for 10 seconds. Block the semaphore meanwhile.".postln;
10.wait;
c.signal;
"thread 1> ok, done. Release the semaphore.".postln;
};
fork {
3.0.rand.wait;
"thread 2> I would like to go on, if I may.".postln;
c.wait; // may I?
"thread 2> this took until the other thread has released the semaphore. "
"Blocking for 4 seconds.".postln;
4.wait;
"thread 2> ok, done. Releasing the semaphore".postln;
c.signal;
};
fork {
4.wait;
"thread 3> I, too, would like to go on, if I may.".postln;
c.wait; // may I?
"thread 3> this took until both other threads had released the semaphore.".postln;
c.signal;
};
)
::
code::
// allow two threads at a time.
(
c = Semaphore(2);
fork {
c.wait;
"thread 1> now I am doing something for 20 seconds. Block the semaphore.".postln;
10.wait;
"thread 1> ok, done. Releasing the semaphore".postln;
c.signal;
};
fork {
rrand(3.0, 5.0).wait;
"thread 2> I would like to go on, if I may.".postln;
if(c.count <= 0) { "thread 3> ok, then I wait ...".postln };
c.wait; // may I?
"thread 1> ok, going ahead.".postln;
17.wait;
"thread 2> ok, done. Releasing the semaphore".postln;
c.signal;
};
fork {
6.wait;
"thread 3> I, too, would like to go on, if I may.".postln;
if(c.count <= 0) { "thread 3> ok, then I wait ...".postln };
c.wait; // may I?
"thread 3> ok, this took until the first thread had released the semaphore. "
"Ok, doing something for 4 seconds. Block the semaphore".postln;
4.wait;
"Releasing the semaphore.".postln;
c.signal;
};
fork {
7.wait;
"thread 4> Me, the fourth one, would like to go on, if I may.".postln;
if(c.count <= 0) { "thread 4> ok, then I wait ...".postln };
c.wait; // may I?
"thread 4> ok, this took until the third thread had released the semaphore. "
"Ok, doing something for 3 seconds. Block the semaphore".postln;
3.wait;
"Releasing the semaphore.".postln;
c.signal;
};
)
::
code::
// grant exclusive access to data to only one thread
// there should never be mixed values in the data array
(
var data, useAndModify;
data = [1, 2, 3];
c = Semaphore(1);
// c = Semaphore(2); use this to test how it would behave without exclusive access.
useAndModify = { |newData, who|
postln(who + "trying to get blocking access.");
if(c.count <= 0) { who + "ok, then I wait ...".postln };
c.wait; // may I access? if not, I wait. if yes, disallow others.
"\n".post;
(who + "continuing...").postln;
data.do({ |x|
0.1.wait;
postln(who + x);
});
"\n".post;
newData.do { |x, i| data[i] = x };
postln(who + "rewriting data to:" + newData);
postln(who + "releasing");
c.signal; // allow others access again
};
// e.g. set the values to integers
u = Routine {
inf.do { |i|
useAndModify.value([100, 200, 300], "thread 1>");
rrand(1, 3).wait;
}
};
// e.g. set the values to floats
k = Routine {
0.5.wait;
inf.do { |i|
useAndModify.value([pi, 0.5pi, 2pi], "thread 2>");
rrand(1, 5).wait;
}
};
u.play;
k.play;
);
::
|