[cpp-threads] Sequentially consistent atomic memory fences

Alan Stern stern at rowland.harvard.edu
Sat Nov 27 03:23:47 GMT 2010


Is there anything missing from 29.3.p3-6?  The specification of the
properties of sequentially consistent fences appears to be incomplete.

Consider the following schematic example:

	atomic_int a, b;

	a.store(0, mo_seq_cst);
	b.store(0, mo_seq_cst);

	Thread 0			Thread 1
	--------			--------
	a.store(1, mo_seq_cst);		b.store(1, mo_seq_cst);
	b.store(2, mo_seq_cst);		a.store(2, mo_seq_cst);

	join threads 0 and 1
	assert(a==2 || b==2);

The draft standard allows one to prove that the assertion will never
fail.  Similarly for the analogous program where the thread routines
are changed to use fences:

	Thread 0			Thread 1
	--------			--------
	a.store(1, mo_relaxed);		b.store(1, mo_relaxed);
	fence(mo_seq_cst);		fence(mo_seq_cst);
	b.store(2, mo_relaxed);		a.store(2, mo_relaxed);

Again, the standard is strong enough to show that the assertion will
always hold.  But now consider this hybrid version:

	Thread 0			Thread 1
	--------			--------
	a.store(1, mo_seq_cst);		b.store(1, mo_relaxed);
					fence(mo_seq_cst);
	b.store(2, mo_seq_cst);		a.store(2, mo_relaxed);

Here the standard allows the assertion to fail.  Why is this?  Is it
deliberate or is it simply an oversight?  If it is deliberate, what is
the justification?

Alan Stern




More information about the cpp-threads mailing list