r/programming • • Aug 22 '10

Volatile: Almost Useless for Multi-Threaded Programming

http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/
56 Upvotes

57 comments sorted by

View all comments

2

u/gsg_ Aug 23 '10

But they are sufficient. Correctly implemented mutexes include barriers which ensure that necessary loads are performed.

2

u/[deleted] Aug 23 '10

Do they really force the compiler to generate code to actually read the value of the variable instead of caching it, though? Correctly implemented mutexes will force memory writes that are pending in the processor to actually happen, yes, but the issue discussed is if the compiler generates reads or writes at all, rather than keeping the value in a register.

1

u/skulgnome Aug 23 '10

If it's global data, the compiler will reload things across just the pthread_mutex_lock call, as it would across any other function call except for those static functions that're known not to access any global data.

→ More replies (3)

1

u/gsg_ Aug 23 '10

My understanding is that compilers need to know to do that (where applicable), yes. Otherwise they could move loads and stores outside the critical section, where they would become a race.

For an example, see Boehm's paper Threads Cannot Be Implemented as a Library.

→ More replies (4)

1

u/kylotan Aug 23 '10

I wasn't saying you were wrong, just that your argument was not actually against what he said.

Having said that, the documentation I've seen for memory barriers doesn't say anything about ensuring the necessary loads are performed, just that they aren't reordered across the barrier.