[chuck-users] Stopping and restart shreds

w31rd0 w31rd0 at mail.ru
Wed Jul 26 07:35:07 EDT 2006


> > > Currently in ChucK, a parent shred will terminate any child shreds
> when
> > > the parent terminates.
> >
> > The current way chuck works makes sense. But (and I don't know "the
> > prober to do this in concurrent programming) I think it would make
> more
> > sense to wait for child shreds to finish before terminating. This
way
> a
> > parent wouldn't have to worry how long the child is running, which I
> > think is none of it's business.
> >
> > I have a lot of examples in my code on me explicitly having to
handle
> > this and it both 1) looks ugly and 2) is not that robust.
> >
> > BTW: would it be possible to grab the id of the child and wait for
> that
> > is to finish, instead of my current way (chucking a sufficiently
large
> > dur to now in the parent after sporking)?
> 
> You can make your children explicitly signal they are exiting, like
> this:
> 
> 
> fun void foo( Event e )
> {
>     10::ms => now;
>     <<< "foo", "" >>>;
>     e.signal();
> }
> 
> fun void bar( Event e )
> {
>     1000::ms => now;
>     <<< "bar", "" >>>;
>     e.signal();
> }
> 
> Event e1, e2;
> 
> spork ~ foo( e1 );
> spork ~ bar( e2 );
> 
> e1 => now;
> e2 => now;
> 
> 
> Still ugly though ;(

Oops!
Discovered that this wouldn't work when "foo" is executed longer than
"bar".
Here is a slightly improved version:


public class Semaphore
{
    0 => int _counter;
    Event _event;

    public void push()
    {
        _counter++;
    }

    public void pop()
    {
        _counter--;
        if( _counter <= 0 )
        {
            _event.signal();
        }
    }

    public void wait()
    {
        _event => now;
    }
}

fun void foo( Semaphore s )
{
    s.push();
    100::ms => now;
    <<< "foo", "" >>>;
    s.pop();
}

fun void bar( Semaphore s )
{
    s.push();
    1000::ms => now;
    <<< "bar", "" >>>;
    s.pop();
}

Semaphore s;

spork ~ foo( s );
spork ~ bar( s );

s.wait();


___________________
w31rd0




More information about the chuck-users mailing list