Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Semaphore puzzle

by Anonymous Monk
on Feb 23, 2021 at 17:40 UTC ( #11128700=note: print w/replies, xml ) Need Help??

in reply to Semaphore puzzle

You need a way to know when you're acquiring or creating the semaphore. So you'd need another semaphore that is already open, and ready to use. Before the above code you need to acquire the semaphore before opening or creating like your code. What is most likely happening is while the first thread is attempting to open/create (most likely create)the second thread comes in and doesn't have to create, but just open the Sem. So you really need a kind of double checked locking.

NON-PERL pseudo code
SemLock = Semaphore->new (....) # some where global in the script if(sem) { SemLock->lock() sem->lock() SemLock->unlock() } else { SemLock->lock() sem= new Semaphore(....) sem->lock() SemLock->unlock() } doStuff() sem->unlock()

Hope this helps, and makes sense


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11128700]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2021-05-17 05:00 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (154 votes). Check out past polls.