[cpp-threads] Sequential Consistency redux

Alan Stern stern at rowland.harvard.edu
Fri Nov 18 15:20:29 GMT 2011


On 18 Nov 2011, N.M. Maclaren wrote:

> I cannot find these rather fundamental questions answered in the
> standard.  They may be there, of course.
> 
> Are the facilities in 30 [Thread] required to be sequentially consistent
> or not as far as ordinary code goes?
> 
> Orthogonally to that, does the same apply to relaxed atomics in the code
> sections protected by mutexes?
> 
> Consider the following example:
> 
>     In serial code:
>         static mutex A, B;
>         int a = 0, b = 0;
>     The program then creates 4 threads.
>     In thread 1:
>         A.lock(); a = 1; A.unlock();
>     In thread 2:
>         B.lock(); b = 1; B.unlock();
>     In thread 3:
>         A.lock(); cout << a << endl; A.unlock();
>         B.lock(); cout << b << endl; B.unlock();
>     In thread 4:
>         B.lock(); cout << b << endl; B.unlock();
>         A.lock(); cout << a << endl; A.unlock();
> 
> Can both threads 3 and 4 print 1 and 0, thus indicating that
> mutex-locked code is not sequentially consistent?  If not, where is that
> stated?

As far as I know, it isn't stated anywhere.  Therefore we must assume 
that sequential consistency is not required for mutexes.

> Obviously, this can be repeated with 'a' and 'b' accessed by relaxed
> atomics.
> 
> The same questions apply to most of section 30.
> 
> 
> Regards,
> Nick Maclaren.

Alan Stern




More information about the cpp-threads mailing list