Title
mutex destructor synchronization
Status
c++11
Section
[thread.mutex.requirements]
Submitter
Jeffrey Yasskin

Created on 2009-09-30.00:00:00 last changed 161 months ago

Messages

Date: 2010-10-21.18:28:33

Proposed resolution:

  • Add a new paragraph after [thread.mutex.class] p1:

    1 The class mutex provides a non-recursive mutex ...

    [Note: After a thread A has called unlock(), releasing the mutex, it is possible for another thread B to lock the same mutex, observe that it is no longer in use, unlock and destroy it, before thread A appears to have returned from its unlock call. Implementations are required to handle such scenarios correctly, as long as thread A doesn't access the mutex after the unlock call returns. These cases typically occur when a reference-counted object contains a mutex that is used to protect the reference count. — end note]

Date: 2009-11-18.00:00:00

[ 2009-11-18: Moved to Tentatively Ready after 5 positive votes on c++std-lib. ]

Date: 2009-09-30.00:00:00

If an object *o contains a mutex mu and a correctly-maintained reference count c, is the following code safe?

o->mu.lock();
bool del = (--(o->c) == 0);
o->mu.unlock();
if (del) { delete o; }

If the implementation of mutex::unlock() can touch the mutex's memory after the moment it becomes free, this wouldn't be safe, and "Construction and destruction of an object of a Mutex type need not be thread-safe" [thread.mutex.requirements] may imply that it's not safe. Still, it's useful to allow mutexes to guard reference counts, and if it's not allowed, users are likely to write bugs.

History
Date User Action Args
2011-08-23 20:07:26adminsetstatus: wp -> c++11
2010-10-21 18:28:33adminsetmessages: + msg1191
2010-10-21 18:28:33adminsetmessages: + msg1190
2009-09-30 00:00:00admincreate