|
Boost : |
Subject: Re: [boost] [thread] On shared_mutex
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2010-11-29 10:28:48
On Nov 28, 2010, at 9:27 PM, Jeffrey Lee Hellrung, Jr. wrote:
> Please excuse the basic concurrent programming questions, I just want to make sure I understand the exposition correctly...
>
> It looks like this implementation of try_lock_until could "fail" under contention, i.e., neither contending thread would perform the averaging computation. Suppose thread 1 calls a1.average(a2) and, simultaneously, thread 2 calls a2.average(a1). It could happen that thread 1 locks a1.mut_ in the first line of try_lock_until, and thread 2 similarly locks a2.mut_, and then each thread is waiting to acquire the other mutex. Now they both time out waiting for the other mutex before either is able to unlock their held mutex, so neither thread enters the average computation. Is this accurate?
Yes, I believe so.
> If so, this seems undesirable, though it isn't nearly as bad as a deadlock scenario. Is it possible to guarantee this won't happen?
Yes, use std::lock instead (as done in the first version of average). std::lock will keep trying as long as it needs to to get both locks locked, and it will not deadlock.
-Howard
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk