From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2007-08-21 16:48:06
On Aug 21, 2007, at 3:22 PM, Yuval Ronen wrote:
> Howard Hinnant wrote:
>> On Aug 21, 2007, at 8:46 AM, Yuval Ronen wrote:
>>> Howard Hinnant wrote:
>>>> Here is a link to a reference implementation and a FAQ for mutexes,
>>>> locks and condition variables I am currently anticipating proposing
>>>> for C++ standardization (or subsequent TR).
>>> After some not-so-thorough reading of this, a few comments:
>>> 1. I couldn't understand what defer_lock is good for, even after
>>> Q.9 of the FAQ. I believe the use-case shown in Q.9 should actually
>>> accept_ownership instead. Can you elaborate please?
>> See if this is any clearer:
> I'm afraid not...
> This example has 3 lines, the first 2 create unique_lock with
> defer_lock, and the 3rd calls std::lock. Those unique_lock don't lock,
> because std::lock to lock. OK. But who unlocks? The unique_locks don't
> own the mutexes, and therefore don't unlock them. But someone needs to
> unlock, and it sounds logical that the unique_locks would... Had we
> accept_ownership, the unique_locks would have owned the mutexes, and
> unlock them. That's the difference between defer_lock and
> accept_ownership, the ownership, isn't it?
Ok, the lightbulb went off in my head and I think I understand your
question now. Thanks for not giving up on me.
I've tried again here:
and see the next question (#10) as well. If that doesn't do it, see:
and search for "defer_lock_type" and "accept_ownership_type" for the
unique_lock implementation of these constructors. Neither constructor
does anything to the mutex, and simply sets the owns() flag to false
or true respectively. The unique_lock destructor will unlock the
mutex iff it owns()'s the mutex.
>>> Either way, I believe this design wouldn't meet the use case which I
>> didn't effectively communicate in #14:
>> Given a read-write mutex and its associated condition variable:
>> my::shared_mutex rw_mut;
>> std::condition<my::shared_mutex> cv(rw_mut);
>> client code wants to wait on that cv in two different ways:
>> 1. With rw_mut read-locked.
>> 2. With rw_mut write-locked.
>> If we initialized the condition variable with cv(rw_mut.exclusive()),
>> then cv.wait() would wait with rw_mut write-locked, but we wouldn't
>> able to wait on cv with rw_mut read-locked.
>> If we initialized the condition variable with cv(rw_mut.shared()),
>> then cv.wait() would wait with rw_mut read-locked, but we wouldn't be
>> able to wait on cv with rw_mut write-locked.
>> This use case desires *both* types of waits on the *same* mutex/cv
> The last sentence starts with "This use case", but I see no use
> case. Do
> we really have such a use case? I haven't seen one yet. But even if we
> had, then maybe the solution is the same solution to the requirement
> phrased as "The freedom to dynamically associate mutexes with
> variables" or "The ability to wait on general mutex / lock types"
> (what's the difference between those two sentences anyway?) in your
> response to Peter. Add a 'set_mutex(mutex_type &)', or maybe even
> 'set_mutex(mutex_type *)' to std::condition. I think it will solve
> rare case.
Ok, perhaps I'll clean the following use case up, and include it in
the faq. First I'll try it out here. :-)
I've got a "communication node" class. It serves as a node in a
network. It gets data from somewhere and temporarily stores it while
it uses several threads to forward (or relay) the information to other
nodes in the network. For simplicity I'm using vector<int> for the
data, and only two relay threads. The example is incomplete (not even
compiled), just illustrative right now:
typedef std::tr2::shared_mutex Mutex;
while (!get_data_ || !data_relayed_ || !
std::copy(from_->begin(), from_->end(), data_.begin());
get_data_ = false;
fresh_data_ = true;
data_relayed_ = false;
data_relayed_ = false;
void relayer(int id)
std::copy(data_.begin(), data_.end(), to[id].begin());
data_relayed_[id] = true;
One thread will be running "supplier", while two other threads will
run "relayer". Additionally some fourth thread (not shown) will tell
"supplier" when there is new data that it needs to go get.
In this design, which may not be the best way to do things, but it
looks like reasonable client-written code to me, there is one
shared_mutex and one condition to control the data flow. The supplier
waits for there to be new data to get, and also waits until the
relayers have done their jobs, before getting new data. It needs
write access to the data. So it holds mut_ write-locked, and waits on
the cv_ with it.
The relayer threads only need read access to the data. So they each
have mut_ read-locked, and wait on the cv until they get the
instruction that it is time to relay the data. Once they relay the
data, they notify everyone else that they're done.
This example is meant to demonstrate a reasonable use case where one
thread wants to wait on a cv with a read-lock while another thread
wants to wait on the same cv/mutex with a write lock. Both readers
and the writer may all be waiting at the same time for there to be
data available from an upstream node (and a fourth thread would have
to notify them when said data is available). Because of this, it is
not possible (in the above use case) for there to be a set_mutex on
the condition to change the facade, since both facades are
simultaneously in use.
Did I make more sense this time? I often complain when people use too
much English and not enough C++ in their arguments, and then I find
myself being guilty of the same thing. :-)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk