?

Log in

No account? Create an account
Work around for lack of locking - Nick [entries|archive|friends|userinfo]
Nick

[ website | gagravarr.org ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Work around for lack of locking [Aug. 12th, 2003|04:28 pm]
Nick
What you want to do:

sub main {
(initiate your threads)

wake up threads with cond_broadcast

wait for them to finish
}

sub thread {
get lock on shared object
cond_wait on locked object

work
}

Only, this causes your code to run one thread at a time. The cond_wait re-locks the lock object, and holds it until you finish. Perl lacks an unlock function.

Solution:

sub thread {
&do-my-locking

work
}

sub do-my-locking {
get lock on shared object
cond_wait on locked object
cond_signal on locked object # triggers other threads
return # will cause an unlock
}

Quite why you can't unlock explicitly is beyond me. Still, the above will get you out of the hole I've been in for a few hours....
linkReply

Comments:
From: hsenag
2003-08-12 09:24 am (UTC)

why not use semaphores?

use Thread::Semaphore;

my $s = new Thread::Semaphore(0);

sub main {
(initiate threads)
$s->up();
(wait for finish)
}

sub thread {
$s->down();
$s->up();
(work)
}
(Reply) (Thread)
From: (Anonymous)
2003-08-12 11:07 am (UTC)

POE!

(Quidity)</a> Use POE, or maybe ithreads, you silly boy. (Or, er, Java :)

(Reply) (Thread)
From: gagravarr
2003-08-13 01:31 am (UTC)

Re: POE!

Hmm, POE looks interesting, will have to investigate
(Reply) (Parent) (Thread)