This is PerlMonks "Mobile"

Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  


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

dk