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
sem= new Semaphore(....)
Hope this helps, and makes sense