[cpp-threads] Memory model question

Peter Dimov pdimov at mmltd.net
Fri Aug 26 21:00:50 BST 2005


>> On 8/26/05, Peter Dimov <pdimov at mmltd.net> wrote:
>>> In your opinion, does the following example contain a data race?
>>>
>>> // initially x == 0
>>>
>>> T1:
>>>
>>> x = 0;
>>>
>>> T2:
>>>
>>> r1 = x;
>>
>> Yes.
>>
>> regards,
>> alexander.

Boehm, Hans wrote:
> I agree with Alexander.
>
> Presumably you are asking the question, because the pthread standard
> uses the word "modify", and it's not clear that x is being modified
> here?

There are two ways to specify the memory model. One approach is to talk 
about reads and writes, under which there is a race. The other alternative 
is to talk about values and their potential visibility, which is more 
consistent with the current language of the standard, where there is no 
concept of (nonvolatile) variable read. Under the second model, there is no 
"race", because the only value that x ever has is 0.

The significance is, as you say, compiler optimizations.

x = 0;

->

x = 1; x = 0; is allowed under (1) and disallowed under (2). Similarly,

if( a ) x = 0; else x = 1;

->

x = 1; if( a ) x = 0;

is disallowed under (2).





More information about the cpp-threads mailing list