[cpp-threads] D2335 (sequential consistency proof) revision
Herb Sutter
hsutter at microsoft.com
Fri Aug 24 23:05:55 BST 2007
Hans wrote:
> > Here's another possible way out of this. I'm not 100% sure this is
> > correct yet, but it seems like it on first glance:
> >
> > We allow trylock and timedlock to fail spuriously, even if the lock
> > was never held.
[...]
> > If we do this, I believe we no longer need acquire semantics for a
> > failed trylock.
Howard wrote:
> It sounds good to me. I'm not an expert in the visibility area so I'd
> like to double check my understanding.
>
> This code which takes two locks/mutexes and locks them without
> deadlock, in part by using try_lock, is not affected by Hans'
> proposal, right?
Right.
> template <class L1, class L2>
> void
> lock(L1& l1, L2& l2)
> {
> while (true)
> {
> l1.lock();
> if (l2.try_lock())
> break;
> l1.unlock();
> l2.lock();
> if (l1.try_lock())
> break;
> l2.unlock();
> }
> }
The only change Hans is proposing is that a failed try_lock doesn't have ordering guarantees, which doesn't affect this example because the try_lock's have nowhere to be reordered to anyway because they can't move out of the tightly enclosing lock/unlock critical sections.
Herb
More information about the cpp-threads
mailing list