Lipika Deka
Lipika Deka

Reputation: 3884

Can a semaphore be initialized multiple times

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

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

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

J-16 SDiZ
J-16 SDiZ

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

Beep beep
Beep beep

Reputation: 19151

If you've freed the memory and destroyed it first, sure.

Upvotes: 0

Related Questions