user1035931
user1035931

Reputation: 21

C++ - init & destory of mutexes and spin-lock

Is it possible to allocate and init a mutex in one thread and destory it in another?

Thanks.

Upvotes: 2

Views: 238

Answers (1)

David Schwartz
David Schwartz

Reputation: 182753

Yes. Mutexes are process resources that are shared between threads. Just make sure there's no way another thread could be accessing the mutex at the time it's destroyed or after.

It is a very common pattern to construct an object with a mutex and then later destroy that mutex when the object is destroyed. It would be very irritating if you had to make sure the same thread destroyed the mutex as created it -- that thread might not even exist any more. If it's a process shared mutex, the process that create it might not even exist any more.

Upvotes: 4

Related Questions