11-13-2015 06:34 AM
hi everybody!
so i started using semaphores quite a bit recently,
and was trying to find a use-case that needed more than one party to acquire the lock.
i couldn't think of one.
my thought is, that if it is only a READ-access, then i don't acquire a lock and jsut read (if i don't care for data integrity),
but WRITE-accesses should always have the lock, so no dirty updates can occur.
i'd appreciate it, if you could think of a sensible example.
greetings
11-13-2015 07:07 AM
If you only need 1 party to acquire the lock, then you do not need a lock. In your example, I would just use an Action Engine and let the non-reentrant VI boundary do the locking for me.
The only real legitimate use of a Semaphore I have ran into was when dealing with an instrument that had a lot of different capabilities (DIO, switches, DMM). I had a different module for each of those capabilities and used the Semaphore to keep each module from stepping on top of the other. Not long after that I learned that VISA has a locking mechanism I could have used instead of the Semaphore that would have made it a little simpler.
11-13-2015 07:09 AM
sorry my writing seems confusing 😞
of course i would have several parties interested in acquiring the lock to one variable,
but why would i want to have (eg) two that hold the lock at the same time?
11-13-2015 07:16 AM
@jwscs wrote:
but why would i want to have (eg) two that hold the lock at the same time?
The point of a Semaphore is to only allow 1 thing access a resource at a time.
11-13-2015 07:43 AM - edited 11-13-2015 07:44 AM
thats exactly my point, but why does the "Obtain Semaphore Reference" allow to set a "size">1
http://zone.ni.com/reference/en-XX/help/371361L-01/glang/obtain_sem_ref/
or is it that i misunderstand the documentation for "size"?
11-13-2015 07:47 AM
i thought about a (not sensible use-case):
it would be possible to control execution flow, several tasks can work while another (one) waits until the locks are released
but it doesn't make mcuh sense (yet) for me regarding the dirty-read/write problem,
11-14-2015 09:40 PM
@crossrulz wrote:
@jwscs wrote:
but why would i want to have (eg) two that hold the lock at the same time?
The point of a Semaphore is to only allow 1 thing access a resource at a time.
Not 1 thing. N things.
I don't have a particular use case in mind (it's been quite a few years since I wrote code with semaphores), but I can certainly imagine cases where you say "I want at most N process to access a particular resource to minimize resource contention".