| 1 |
// Listing 10.4 Using one shred to signal() multiple other shreds |
| 2 |
|
| 3 |
// Declare an event we will use for signaling |
| 4 |
Event evnt; // (1) Declares an event. |
| 5 |
|
| 6 |
// function that waits on an event |
| 7 |
fun void bar(Event anEvent, string msg, float freq) // (3) Frequency for pop. |
| 8 |
{ // (2) New event-waiting function with extra arguments. |
| 9 |
Impulse imp => ResonZ rez => dac; // (4) Tuned pop sound. |
| 10 |
50 => rez.Q; // (5) High resonance for tuned pop. |
| 11 |
while( true ) // (6) Infinite loop |
| 12 |
{ |
| 13 |
// wait |
| 14 |
anEvent => now; // (7) Sleeps until event is signaled. |
| 15 |
// action |
| 16 |
<<< msg, freq, now / second >>>; // (8) When event comes, print out. |
| 17 |
freq => rez.freq; // (3) Frequency for pop. |
| 18 |
50 => imp.next; // (9) Triggers pop sound. |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// spork a few bar shreds |
| 23 |
spork ~ bar( evnt, "Hi ", 500.0 ); // (10) Spork one event-waiting function |
| 24 |
spork ~ bar( evnt, "There ", 700.0 ); // (11) ...and another, with different string and frequency... |
| 25 |
spork ~ bar( evnt, "Sport! ", 900.0 ); // (12) ...and yet another, different from the other two. |
| 26 |
|
| 27 |
// then signal the event forever in a loop |
| 28 |
while( true ) // (13) Infinite loop to test sporked functions. |
| 29 |
{ |
| 30 |
// fire the next signal |
| 31 |
evnt.signal(); // (14) Fire event using signal() method. |
| 32 |
// advance time |
| 33 |
1::second => now; |
| 34 |
} |