Reputation: 3884
Is it ok to initialize a semaphore any number of times within a program after being used so that it can be used again.
Thanks
Upvotes: 1
Views: 1598
Reputation: 215261
You can destroy and reinitialize a POSIX semaphore as long as there are no threads waiting on it and there is no possibility that another thread could attempt to use it during the interval it's invalid (destroyed). However, I suspect the motivation to do so comes from a misunderstanding of what you're trying to accomplish...
Upvotes: 3
Reputation: 26910
If you are asking the POSIX semaphore you asked here, just do a sem_post
/ sem_wait
-- you don't have to initialize them again.
If you must do it (e.g. want to cancel the current value), destroy it first.
Upvotes: 1