From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

semaphore stolen

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.  

 

Comments?  

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 7
(2,686 Views)

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.


Lifespan 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.

 

Lynn

 

Message 2 of 7
(2,671 Views)

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.   

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 3 of 7
(2,653 Views)

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.

 

Lynn

Message 4 of 7
(2,645 Views)

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.  

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 5 of 7
(2,619 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 6 of 7
(2,590 Views)

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.  

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
Message 7 of 7
(2,573 Views)