Boost logo

Threads-Devel :

From: Anthony Williams (anthony_at_[hidden])
Date: 2008-05-03 17:56:37


Quoting Dmitriy V'jukov <dvyukov_at_[hidden]>:

> boost version 1.35.0
>
> basic_timed_mutex::try_lock() implemented as:

[skip implementation]

> Why it always executes at least one
> BOOST_INTERLOCKED_COMPARE_EXCHANGE? In documentation I don't see any
> requirements that try_lock() have to synchronize memory in the case of
> failure. Why it's not implemented as:
>
> bool try_lock()
> {
> long old_count=active_count;
> while(!(old_count&lock_flag_value));
> {
> long const current_count =
> BOOST_INTERLOCKED_COMPARE_EXCHANGE
>
> (&active_count,(old_count+1)|lock_flag_value,old_count);
> if(current_count==old_count)
> {
> return true;
> }
> old_count=current_count;
> }
> return false;
> }

No particular reason. Thanks for the suggestion. Actually, I've been
intending to change the implementation to use a BitTestAndSet
instruction where it can, so try_lock becomes:

    return !bit_test_and_set(&active_count,lock_flag_bit);

But even then, it might be worth adding a simple non-interlocked read
before-hand to check for the flag, and only do the BTS if it's not set.

Thanks,

Anthony

-- 
Anthony Williams            | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk