| 1 |
// Listing 10.4b Using one shred to signal() multiple other shreds |
| 2 |
|
| 3 |
// Declare an event we will use for signaling |
| 4 |
Event evnt; |
| 5 |
|
| 6 |
// function that waits on an event |
| 7 |
fun void bar(Event anEvent, string msg, float freq) |
| 8 |
{ |
| 9 |
Impulse imp => ResonZ rez => dac; |
| 10 |
50 => rez.Q; |
| 11 |
while( true ) |
| 12 |
{ |
| 13 |
// wait |
| 14 |
anEvent => now; |
| 15 |
// action |
| 16 |
<<< msg, freq, now / second >>>; |
| 17 |
freq => rez.freq; |
| 18 |
50 => imp.next; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// spork a few bar shreds |
| 23 |
spork ~ bar( evnt, "Hi ", 500.0 ); |
| 24 |
spork ~ bar( evnt, "There ", 700.0 ); |
| 25 |
spork ~ bar( evnt, "Sport! ", 900.0 ); |
| 26 |
|
| 27 |
// then signal the event forever in a loop |
| 28 |
while( true ) |
| 29 |
{ |
| 30 |
// fire three signals at once |
| 31 |
evnt.signal(); |
| 32 |
evnt.signal(); |
| 33 |
evnt.signal(); |
| 34 |
// advance time |
| 35 |
1::second => now; |
| 36 |
} |