LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What is protected by a LabVIEW Semaphore?

I can't seem to find a lot of documentation on LabVIEW Sempahores. I was wondering whether there is any firm guarantee as to what is protected by a semaphore and what is not protected by a semaphore.

The only example I could find seemed to indicate that calls to a SubVI are protected if they are made from within the confines of an acquired semaphore. I was wondering whether attempts to write to a local variable or to a global variable [from within the confines of an acquired semaphore] were also guaranteed to be protected.

I've attached a sample VI which has four threads: Three of them try to write about 100 times per second, and the fourth tries to read what they've written about 1000 times per second.

The three w
riter threads write -1, 0, and 1, respectively, to each of three data structures: a local variable, a global variable, and a "functional global variable" within a SubVI.

I was wondering whether there is any firm guarantee that all three of these kinds of writes are protected by semaphores, or if it's just the write to the SubVI that's protected.

As an aside, the CPU time allotted to the three writer threads doesn't seem to be distributed very evenly; one thread will grab the CPU and hold onto it for much longer than it ought to [given that it should be awake for only about 1/100th of a second at a time]. Is there any way to force the CPU time to be distributed more evenly?

PS: The attached VI does no error checking; I just wanted to throw together something that would demonstrate the questions I'm wondering about.

Thanks!
0 Kudos
Message 1 of 2
(2,606 Views)
The help files say that the code between Acquire and Release is protected. In your code you do not verify that the semaphore was actually acquired (no timeout and no error). I changed your VI to use a state machine structure for each "thread" and included error checking. The semaphore is only released if it was acquired (Help says an error may occur otherwise).

I have found that the Synchronization functions and VIs require some experimentation to really understand how they work.

Lynn
0 Kudos
Message 2 of 2
(2,606 Views)