[cpp-threads] [Javamemorymodel-discussion] there's a happens-before orderhere. right?

Anthony Williams anthony at justsoftwaresolutions.co.uk
Mon Dec 15 17:37:12 GMT 2008


At Mon 15 Dec 2008 17:23:23 UTC, Alexander Terekhov  
<alexander.terekhov at gmail.com> wrote:

> On Mon, Dec 15, 2008 at 3:47 PM, Anthony Williams
> <anthony at justsoftwaresolutions.co.uk> wrote:
>> At Mon 15 Dec 2008 14:38:30 UTC, Alexander Terekhov
>> <alexander.terekhov at gmail.com> wrote:
>>
>>> On Mon, Dec 15, 2008 at 2:58 PM, Anthony Williams
>>> <anthony at justsoftwaresolutions.co.uk> wrote:
>>>>
>>>> At Mon 15 Dec 2008 13:32:56 UTC, Alexander Terekhov
>>>> <alexander.terekhov at gmail.com> wrote:
>>>>
>>>>> On Mon, Dec 15, 2008 at 12:47 PM, Anthony Williams
>>>>> <anthony at justsoftwaresolutions.co.uk> wrote:
>>>>>>
>>>>>> At Sat 13 Dec 2008 14:42:43 UTC, Alexander Terekhov
>>>>>> <alexander.terekhov at gmail.com> wrote:
>>>>>>>
>>>>>>>  int data; //= 0
>>>>>>>  atomic<int> x; //= 0
>>>>>>>
>>>>>>>  thread 1:
>>>>>>>  ------------
>>>>>>>  if (x.load(relaxed) > 0)
>>>>>>>    data = 1;
>>>>>>>
>>>>>>>  thread 2:
>>>>>>>  ------------
>>>>>>>  data = 2;
>>>>>>>  x.store(+1, release);
>>>>>>>

[snip]

>> It doesn't matter what operation thread 1 is doing on data.
>
> And that is actually the key reason of inefficiency in the model.

Maybe, but that's how it is at the moment. Are you planning on writing  
a paper that will change that?

>> Thread 2 is doing a write, so if thread 1 does *any* operation on data
>> without ordering, you have a data race.
>
> But there's ordering here... think of it as having an implied (due to
> control dependency) "atomic_thread_fence(memory_order_acquire)" (see
> 29.6 Fences in the current draft) affecting stores but not loads.
>
>    int data; //= 0
>    atomic<int> x; //= 0
>
>    thread 1:
>    ------------
>    data = 1;
>    x.store(1, release);
>
>    thread 2:
>    ------------
>    if (x.load(relaxed)) {
>      atomic_thread_fence(memory_order_acquire); // redundant
>      data = 2;
>    }

Under the current C++0x memory model, the fence is not redundant, it  
is required. Control dependencies don't create happens-before  
relationships between threads; only release-acquire pairings can do  
that.

On some current processor architectures it is not required, but that's  
a different issue.

Are you planning to write a paper that proposes changing the C++0x  
memory model to automatically add such orderings?

Anthony
-- 
Anthony Williams
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Just Software Solutions Ltd, Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK





More information about the cpp-threads mailing list