[cpp-threads] Sequential Consistency redux

N.M. Maclaren nmm1 at cam.ac.uk
Fri Nov 18 13:45:17 GMT 2011


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?

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.




More information about the cpp-threads mailing list