Boost logo

Threads-Devel :

From: Dmitriy V'jukov (dvyukov_at_[hidden])
Date: 2008-05-03 17:22:13


boost version 1.35.0

basic_timed_mutex::try_lock() implemented as:

           bool try_lock()
           {
               long old_count=active_count&~lock_flag_value;
               do
               {
                   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;
               }
               while(!(old_count&lock_flag_value));
               return false;
           }

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;
           }

Dmitriy V'jukov


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