I was working on a section of code, and I suspected that there could be a racing condition, so I used a semaphore. After the the semaphore was created (only one), I would wait for the semaphore and release it right after at different section of code. However, at one section of my code, it will wait indefinitely. It was claiming that somewhere else already got the semaphore. that can't be, since I always release the semaphore. I thought of the following, but may not be vaild at all, but they were easy to fix. Any idea?
1. When I created the semaphore, I didn't give it a name. When I stopped the program improperly, perhaps, the same semaphore was used and it was already taken? I just use the current time stamp as the name of the semaphore. Hopefully that will fix it. Is that a possible problem?
2. Upstream of release semaphore, there is an error, and the release semaphore was not executed? I didn't do anything about that, since looking at the release semaphore VI, I don't see that possibilty as vaild.
Creating a semaphore reference and acquiring the semaphore are two different actions. Similarly, releasing the sempahore and releasing the semaphore reference are different. Here is the section of the Detailed Help on semaphores which describes the lifetime of a semaphore.
A semaphore remains in memory as long as a top-level VI that is not idle owns a reference to the semaphore. If the top-level VI becomes idle, LabVIEW releases all semaphore references the VI owns, including references that exist in subVIs of the top-level VI. If LabVIEW releases the last reference to a named semaphore, LabVIEW destroys the semaphore. Because you can obtain only one reference to an unnamed semaphore, LabVIEW destroys an unnamed semaphore when the top-level VI becomes idle. To use a semaphore across multiple top-level VIs, name the semaphore and call the Obtain Semaphore Reference VI from each top-level VI so that each VI obtains its own unique reference to the semaphore.
Without seeing your code we have no way to tell what is happening, but my guess would be that the semaphore is destroyed before you try to acquire it again.
I understand the differences. Perhaps, I used incorrect terminoloties in my previous post. When i was running th debug mode, it sahows that the semaphore reference was still valid. It also said that 1 is waiting semaphore and 0 semaphore was avaiilable, so I know that the reference was not destoryed.
The help for Release Semaphore says it will run even if an input error exists, so that should not be a problem.
If you create a parallel loop with a short Wait and Get Semaphore Status.vi, you may be able to get a clue as to when the mystery semaphore is acquired. Then you can try to determine which section of your code was running at that time.
These things can be tricky to find.
The information is already available when I use a probe. I just have trouble confirm where is the semaphore taken, so that I can determine why it is not released.
Sounds like you are to the point where you need to supply some code if you want any more help.
Try to reduce what code you have as much as you can and still show the issue. You will likely find your problem while going through that exercise.
I figured it out. Two bugs.
1. One thread is not releasing semaphore. The release semaphore is in a case structure selected by an AND logic, the timeout output from acquriing semaphore and test logic. The test logic is causing the AND logic to become false.
2. I put all my semaphore functions (init, wait, release, and end) in a functional global. After thread 1 got the semaphore and before it can release it with the FG, thread 2 started to wait on the semaphore indefinitely. This way, the thread 1 cannot release the semaphore with the FG, since thread 2 is waiting on the semaphore with the same FG.