Boost logo

Boost :

Subject: Re: [boost] [spinlock] Spin on volatile read, NUMA fairness?
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2014-12-03 23:08:54


On 4/12/2014 14:39, Peter Dimov wrote:
> The paper doesn't mean that though. It says that the typical spinlock
> acquisition is:
>
> // atomic_flag f_;
>
> while( f_.test_and_set( std::memory_order_acquire ) )
> {
> /* maybe yield */
> }
>
> and that it's better to do this:
>
> // atomic_bool f_;
>
> do
> {
> while( f_.load( std::memory_order_relaxed ) )
> {
> /* maybe yield */
> }
> }
> while( f_.exchange( true, std::memory_order_acquire ) );
>
> instead.

Out of curiosity, while I agree that the latter is better than the
former, how would this compare:

      while( f_.exchange( true, std::memory_order_acquire ) )
      {
          while( f_.load( std::memory_order_relaxed ) )
          {
              /* maybe yield */
          }
      }

? The only difference is that this assumes that acquiring the lock
should succeed most of the time, so it skips the initial speculative
relaxed load. It still avoids spinning directly on the exchange.

(Also please correct me if I'm wrong but I thought on x86 at least
relaxed and acquire have similar performance anyway, so there's no
benefit to doing an initial relaxed read.)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk