Topic: condition_variable_any works with mutexes, not locks?


Author: Scott Meyers <usenet@aristeia.com>
Date: Tue, 30 Jun 2009 09:33:26 CST
Raw View
30.5/1 includes:

    Class condition_variable_any provides a general condition variable that
    can wait on objects of user-supplied lock types.

So condition_variable_any works with locks.  But 30.5.2/1 says that Lock
types to be used with condition_variable_any

    shall meet the requirements for a Mutex type, except that try_lock is not
    required.

So the "locks" used with condition_variable_any must fulfill (most of) the
requirements for mutexes.  Among those are existence of the member
functions "lock" and "unlock."

30.4.3/1 says that

    A lock is an object that holds a reference to a mutex and may unlock the
    mutex during the lock's destruction (such as when leaving block scope).

The first lock type listed in the "Locks" section of the standard
is lock_guard (30.4.3.1), which lacks both lock and unlock member
functions.

All this suggests to me that the use of the word "Lock" in the
specification of condition_variable_any should be replaced with "Mutex".
For example, 30.5.2/1 should read (my emphasis)

    A *MUTEX* type shall meet the requirements for a Mutex type, except that
    try_lock is not required.

and the wait functions should be prototyped this way

    template <class Mutex> void wait(Mutex& mutex);

    template <class Mutex, class Predicate>
    void wait(Mutex& mutex, Predicate pred);

and the rest of the specification should be changed to refer to mutexes
instead of locks.

Am I overlooking something?

Thanks,

Scott

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Anthony Williams <anthony.ajw@gmail.com>
Date: Tue, 30 Jun 2009 17:57:51 CST
Raw View
Scott Meyers <usenet@aristeia.com> writes:

> All this suggests to me that the use of the word "Lock" in the
> specification of condition_variable_any should be replaced with "Mutex".
> For example, 30.5.2/1 should read (my emphasis)
>
>    A *MUTEX* type shall meet the requirements for a Mutex type, except that
>    try_lock is not required.
>
> and the wait functions should be prototyped this way
>
>    template <class Mutex> void wait(Mutex& mutex);
>
>    template <class Mutex, class Predicate>
>    void wait(Mutex& mutex, Predicate pred);
>
> and the rest of the specification should be changed to refer to mutexes
> instead of locks.
>
> Am I overlooking something?

The use of the term Mutex would have undesirable connotations. The thing
that is passed to condition_variable_any is a "Lockable" object ---
i.e. it has lock() and unlock() member functions. std::mutex and
std::unique_lock<SomeType> fulfil these requirements, as would a
user-written mutex, and a user-written lock wrapper such as
boost::shared_lock<>.

However, I agree that the wording needs clarification and should at
least be self-consistent.

Anthony
--
Author of C++ Concurrency in Action | http://www.manning.com/williams
just::thread C++0x thread library   | http://www.stdthread.co.uk
Just Software Solutions Ltd         | http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]