in reply to Re^2: Help on Understanding Locks in Multithreading
in thread Help on Understanding Locks in Multithreading
What are you expecting, and what did you get which doesn't meet with your expectation ?
FWIW, one wrinkle with threads is that the return context for the thread is set at threads->new time, not on the context of $thr->join(). Your code:
should be:$thr = threads->new(\&sub1, "THREAD1 :"); ... @ReturnData = $thr->join; ...
to provide the right context at the right time.($thr) = threads->new(\&sub1, "THREAD1 :"); ... @ReturnData = $thr->join; ...
The fragment:
is reasonably plausible. You're locking $foo for just long enough to give it a new value -- so there is no possibility of some other thread attempting to read or change $foo while it's in any intermediate state -- provided they too lock $foo before doing anything with it. (The lock is dropped at the end of the block it is contained in.){ lock $foo; $foo=0; }
I note that $foo is not initialised to anything.
I note that you:
without locking it. But that's probably OK, because $foo is only changed by the same thread.print " Final value :$foo\n";
I wonder: you're not expecting $foo to work as some kind of semaphore, are you ?
|
|---|