Opened 6 years ago

Closed 6 years ago

#12620 closed Support Requests (worksforme)

Make boost::unique_lock::unlock respect BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED

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

Description (last modified by viboes)

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.

Attachments (1)

lock_types.patch (545 bytes ) - added by Alexandre Pereira Nunes <alexandre.nunes@…> 6 years ago.

Download all attachments as: .zip

Change History (4)

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

Attachment: lock_types.patch added

comment:1 by viboes, 6 years ago

Description: modified (diff)
Owner: changed from Anthony Williams to viboes

comment:2 by viboes, 6 years ago

Severity: ProblemNot Applicable
Type: BugsSupport Requests

The implementation corresponds to the documentation and I believe to the standard library as well (http://en.cppreference.com/w/cpp/thread/unique_lock/unlock).

comment:3 by viboes, 6 years ago

Resolution: worksforme
Status: newclosed
Note: See TracTickets for help on using tickets.