bucky0 has asked for the wisdom of the Perl Monks concerning the following question:

Brethren-

I've been scratching my head at this for a couple days, perhaps your collective wisdom can shed some light on my problem.

I am trying to use cond_wait for synchronisation, but am either failing or hitting race conditions (depending on how I attempt to do it). Here is some example code:
use strict; use warnings; # hallelujah use threads; use threads::shared; # threads, oh how I hate my $lockvar : shared = 0; my $thrHandle = threads->create( &exampleSub ); {lock $lockvar; cond_wait ($lockvar);} # code that should be executed at the same time as the # block below $thrHandle->join(); sub exampleSub { # some initialisation which may, or may not, take a lot # of time {lock $lockvar; cond_signal( $lockvar );} # code that should be executed at the same time as the # block above }
This is a grossly simplified example, but it should show the problem. If the cond_signal is reached before the other thread reaches the cond_wait, the signal is discarded and the cond_wait will block forever.

The threading POD states that careful locking will prevent this from happening, but no example is provided and I can't figure it out myself.

Thanks for your help!

updated: Added the lock on $lockvar

Replies are listed 'Best First'.
Re: Blocking cond_signal until someone cond_waits
by dave_the_m (Monsignor) on Dec 19, 2006 at 19:33 UTC
    Well, to use the cond_wait stuff, the parent should lock before creating the child. This will work:
    use strict; use warnings; # hallelujah use threads; use threads::shared; # threads, oh how I hate my $lockvar : shared = 0; my $thrHandle; { lock $lockvar; $thrHandle = threads->create( \&exampleSub ); cond_wait ($lockvar); print "parent running\n"; } $thrHandle->join(); sub exampleSub { print "child init\n"; sleep 2; print "child init finished\n"; { lock $lockvar; cond_signal($lockvar); } print "child running\n"; }
    However, that is the hard way. The cond_* functions are very low-level, and for most purposes you should use a higher-level abstraction: e.g., create a queue, and signal by pushing an item onto the queue:
    use strict; use warnings; # hallelujah use threads; use threads::shared; # threads, oh how I hate use Thread::Queue; my $q = Thread::Queue->new; my $thrHandle = threads->create( \&exampleSub ); $q->dequeue; print "parent running\n"; $thrHandle->join(); sub exampleSub { print "child init\n"; sleep 2; print "child init finished\n"; $q->enqueue(0); print "child running\n"; }

    Dave

Re: Blocking cond_signal until someone cond_waits
by BrowserUk (Patriarch) on Dec 19, 2006 at 18:12 UTC

    You need to read the docs a little more closely. From the POD (their emphasis):

    The cond_wait function takes a locked variable as a parameter,

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Oh, of course, in my actual code I have a lock on the lockvar. I just threw that together to further demonstrate what I was wanting to do.