Opened 6 years ago

Last modified 6 years ago

#12620 closed Support Requests

Make boost::unique_lock::unlock respect BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED — at Initial Version

Reported by: Alexandre Pereira Nunes <alexandre.nunes@…> Owned by: Anthony Williams
Milestone: To Be Determined Component: thread
Version: Boost 1.62.0 Severity: Not Applicable
Keywords: Cc:

Description

For a C++11 conformant g++ with working C++11 threads, the following code works if LIB is defined to std and throws if defined to boost::

#define LIB boost

LIB::mutex mtx; LIB::unique_lock<LIB::mutex> l(mtx, LIB::defer_lock); LIB::condition_variable cnd; cnd.wait(l, []{ /*whatever*/ return true;}); this throws.

The attached patch makes boost::unique_lock::unlock respect BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED and not require the lock to be held (if you'll ever have only one thread waiting on a condition variable it seems that the mutex is useless as there's no race involved, c++11 threads at least when backed by pthread seems to follow that behavior).

Even with this patch, boost *will* lock the mutex on exit - I didn't attempt to change that (as my code copes with that), but c++11 seems to avoid touching the mutex or correctly leaves it as it once was.

I'm not sure what/if any of this behavior is standardized or implementation dependent, but it seems more coherent to respect BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED - specially the common case where it's not defined.

Change History (1)

by Alexandre Pereira Nunes <alexandre.nunes@…>, 6 years ago

Attachment: lock_types.patch added
Note: See TracTickets for help on using tickets.