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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|