[chuck-users] Events

Andrew Turley aturley at acm.org
Wed Nov 10 00:45:20 EST 2010


In you're example you're creating two separate events, one in the main
shred and one in the call to function f(). These events have the same
type, but they won't communicate because they are different events.
The way events work is that one or more shreds wait on the event, and
then another shred uses the event's signal() method to wake up the
first shred in the queue, or the broadcast() method to wake up all of
the shreds.

This slight modification to your code does work:

// BEGIN CODE
class MyEvent extends Event {
string s;
} // class MyEvent

MyEvent e;

spork ~ f(e);

e => now; // wait for it
// this is never printed and this shred never exits
<<<"Event occured.">>>;
me.exit();

function void f(MyEvent e) {
while (true) { // fire one event per second
e.broadcast ();
1::second => now;
} // while

//<<<"Broadcast...">>>;
} // f
// END CODE

I've created an event e before sporking on function f(), and f() now
takes an event as an argument. This way, the main shred and the
sporked shred communicate using the event e.

andy

On Tue, Nov 9, 2010 at 9:21 PM, Rich Caloggero <rjc at mit.edu> wrote:
> Why doesn't this simple code display any messages?
>
> class MyEvent extends Event {
> string s;
> } // class MyEvent
>
> spork ~ f();
>
> MyEvent e;
> e => now; // wait for it
> // this is never printed and this shred never exits
> <<<"Event occured.">>>;
> me.exit();
>
> function void f() {
> MyEvent e;
> while (true) { // fire one event per second
> e.broadcast ();
> 1::second => now;
> } // while
>
> //<<<"Broadcast...">>>;
> } // f
>
> Thanx muchly as always. <smile>
> -- Rich
>
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>


More information about the chuck-users mailing list