Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: What's difference between memory_order_relaxed and no memory order in c++0x?

Reply
Thread Tools

Re: What's difference between memory_order_relaxed and no memory order in c++0x?

 
 
Anthony Williams
Guest
Posts: n/a
 
      09-14-2010
kuangye <> writes:

> What's difference between memory_order_relaxed and no memory order in c
> ++0x?
>
> /*
> The following code sample is from
> http://www.justsoftwaresolutions.co....onization.html
> */
>
> std::atomic<int> x(0),y(0);
>
> void thread1()
> {
> x.store(1,std::memory_order_relaxed);
> y.store(1,std::memory_order_relaxed);
> }
>
> void thread2()
> {
> int a=y.load(std::memory_order_relaxed);
> int b=x.load(std::memory_order_relaxed);
> if(a==1)
> assert(b==1); // no guarantee that b==1 when a==1
> }
>
> std::thread t1(thread1);
> std::thread t2(thread2);
>
>
> --------------------------------------------------------------------------------------------
> If I replace the atomic<int> with int ( as the following code). what
> will happen??


Undefined behaviour. You have two non-atomic accesses to a variable, one
of which is a write, with no defined order between them. This is
therefore a data race and undefined behaviour.

> Therefor, what's difference between memory_order_relaxed and no memory
> order in c++0x?


The use of atomics with memory_order_relaxed make it defined
behaviour. With atomics, the value of x is either 0 or 1, the same for
y. With plain int the program has undefined behaviour and may format
your hard disk.

> Does memory_order_relaxed prevent c++ compiler from code reordering or
> anything else ??


It ensures that the compiler uses the correct instructions for atomic
operations on the target CPU. It also may potentially inhibit some
optimizations.

Anthony
--
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++0x thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57