| 1 |
// 143.ck : exiting while child shreds waiting on event |
| 2 |
|
| 3 |
// declare an event |
| 4 |
Event e; |
| 5 |
|
| 6 |
// declare function |
| 7 |
fun void hi( Event e, int id ) |
| 8 |
{ |
| 9 |
while( true ) |
| 10 |
{ |
| 11 |
// wait on event e |
| 12 |
e => now; |
| 13 |
// print id |
| 14 |
<<< id, "" >>>; |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
// spork shred with e |
| 19 |
spork ~ hi( e, 1 ); |
| 20 |
spork ~ hi( e, 2 ); |
| 21 |
spork ~ hi( e, 3 ); |
| 22 |
|
| 23 |
// yield() to let the sporked shreds run and wait on event |
| 24 |
me.yield(); |
| 25 |
|
| 26 |
// signal e |
| 27 |
e.broadcast(); |
| 28 |
|
| 29 |
// yield() again to give others shreds a chance to receive event trigger |
| 30 |
me.yield(); |
| 31 |
|
| 32 |
<<< "success" >>>; |
| 33 |
|
| 34 |
// after this the parent shred will free the child shred |
| 35 |
// if nothing else gets printed (e.g., some kind of a crash!), |
| 36 |
// then call it actual success |