01-24-2006 11:34 AM
01-24-2006 11:50 AM
01-24-2006 12:00 PM
The refnum itself is not enough to keep the semaphore. It is only a "pointer" to it. Once the top level VI stops running, LV does automatic garbage collection and destroys the semaphores created by the hierarchy, even if the subVI which created the semaphore is still in memory. You still have the pointer, but it is now worthless.
I'm not sure myself whether this behavior is correct. Perhaps a semaphore should not be closed if VIs accessing it are still in memory. It sounds reasonable, but I haven't put the same amount of thought into it which I hope the NI people have.
01-24-2006 12:07 PM
Once the pointer is worthless, the semaphore vi will return an arrar which can be used to stop the subvi, I do this all the time with queues, where thare are several loops which are using the queue to communicate, when I stop destroy the queue from another "top level vi" or even any of the subvi then I have an event to stop all vi's which depend on the resource.
01-24-2006 12:24 PM
Paul, the original poster wants to keep the semaphore, since the top level VI is only used to call the other VI dynamically and then it is closed. Using a pure G approach to get the refnums resolves this problem, since the refnums remain valid.
The other option is to keep the top level VI in memory by opening a reference to itself and hiding it FP, so it's not in the way.
01-24-2006 02:57 PM
Lets see if I can add some value without getting stuck!
There is a size input fo rthe create semaphore.
That determines how many enities can get the semaphore.
When a semaphore is requested and obtained, that value gets decremented. When the value is zero, nobody else get the semaphore. When the semaphore is released tha value is incremented.
This how LV tracks if the semhpre is in use.
When a VI evaluated to determine if it can be unloaded the resource taht were allocated by that VI are checked to see if they are in use. That is done by looking a the semaphore count and the max size.
So...
1) When the semaphore is created set the size = 2
2) Imediately after creating it, acquire the semaphore. (that should leave one for everyone else).
3) Everything else codes the same until the end.
4) At shutdown time, the entity that will destroy the semaphore should do a "release" prior to the destruction.
I have not tested this but this is how I understand it and what I would try if I was faced with this challenge.
Ben
01-24-2006 04:12 PM
No, Ben, that doesn't work.
By the way, this behavior is very clearly stated in the help for Create Semaphore:
01-24-2006 04:17 PM
Thanks tst!
Ben