[cpp-threads] Visibility question

Boehm, Hans hans.boehm at hp.com
Tue Aug 1 23:27:37 BST 2006


Clark brought up the following interesting question that I couldn't
immediately answer.  I think I now have the answer, but I thought I
would post it here to make sure we agree.

The question is whether "raw", i.e. unordered, atomic operations should
contribute to the happens-before relation.

As a particular example of what I mean, consider (x,y,z atomic, w
ordinary, everything initially zero):

Thread 1:
w = 1;	// ordinary store
y.fetch_add<ordered>(1);		// ordered atomic; orders
everything within thread
x.store<raw>(1);

Thread 2:
r2 = x.load<raw>();
z.fetch_add<ordered>(1);		// ordered atomic; orders
everything within thread; different variable
if (r2) r1 = w;	// ordinary load

Is there a data race on w?

Note that in all versions of the current draft proposal, the statements
within each thread are ordered by inter-thread-ordered-before and hence
happens-before.  (Unlike Java, the intra-thread sequencing order doesn't
automatically contribute to happens-before, but the ordered atomics have
that effect in this case.)

In the current draft proposals, there would be a synchronizes-with (or
communicates-with) relationship introduced by both load-store pairs.
Hence there would be no race.  The ordered fetch_adds and the raw
operations conspire to introduce a happens-before edge.

In the alternative formulation, the raw operations do not introduce a
synchronizes-with relationship, which means there are no happens-before
edges between threads, and there is a race.

My intuition is that the current formulation is right.  Opinions?

(With the formulation on the web, I think that eliminating the
happens-before edge between raw operations adds other problems.  With
the draft we're working on, I think we're down to just problems like the
one above.)

Hans



More information about the cpp-threads mailing list