On 01/23/2013 03:08 PM, David Faure wrote: > I was talking to Julian about this again today, and he pointed me to this > writeup: > > http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming > > We're looking at how to silence valgrind about Qt atomic ops, but before that, > it would actually be good to be sure that what Qt does it correct, on x86.... > > Where does the claim about "volatile cast means the compiler can't reorder > stuff around them" > come from?
I think that claim is incorrect. It may be that the compiler is not allowed to reorder volatile stores, but for sure it can reorder volatile stores w.r.t. non-volatile stores, which makes the volatile stores completely useless from a multithreading perspective. That writeup contains a nice example: volatile int Ready; int Message[100]; void foo( int i ) { Message[i/10] = 42; Ready = 1; } Imagine that 'message' is some chunk of info that we intend to give to another thread, and 'Ready' is a flag that we intend to use to tell the other thread when the message is ready to be read. gcc-4.7.2 -m32 -O2 produces this assembly (omitting the CFI junk): foo: movl 4(%esp), %ecx movl $1717986919, %edx movl $1, Ready <---------- Ready = 1; movl %ecx, %eax imull %edx sarl $31, %ecx sarl $2, %edx subl %ecx, %edx movl $42, Message(,%edx,4) <--------- Message[i / 10] = 42; ret so the volatile clearly didn't have the claimed effect. Bottom line is, as I understand it, that C++11 allows implementations (compiler + hardware) to assume code is race free, and optimise accordingly. On x86, we have the added guarantee that the hardware will deliver stores in order, but the compiler is still free to reorder ordinary stores. J ------------------------------------------------------------------------------ Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS, MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft MVPs and experts. ON SALE this month only -- learn more at: http://p.sf.net/sfu/learnnow-d2d _______________________________________________ Valgrind-users mailing list Valgrind-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/valgrind-users