Can a semaphore be initialized multiple times
Is it ok to initialize a semaphore any num开发者_运维百科ber of times within a program after being used so that it can be used again.
Thanks
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...
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.
If you've freed the memory and destroyed it first, sure.
精彩评论