1218. mutex destructor synchronization

Section: 33.4.3 [thread.mutex.requirements] Status: C++11 Submitter: Jeffrey Yasskin Opened: 2009-09-30 Last modified: 2016-02-10

Priority: Not Prioritized

View other active issues in [thread.mutex.requirements].

View all other issues in [thread.mutex.requirements].

View all issues with C++11 status.

Discussion:

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" 33.4.3 [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.

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

Proposed resolution: