views:

110

answers:

3

I am reading these days about memory fences and barriers as a way to synchronize multithreaded code and avoid code reordering.

I usually develop in C++ under Linux OS and I use boost libs massively but I am not able to find any class related to it. Do you know if memory barrier of fences are present in boost or if there is a way to achieve the same concept? If not what good library can I have a look to?

+1  A: 

There is a boost::barrier class/concept but it's a bit high level. Speaking of which, why do you need a low level barrier? Synchronization primitives should be enough, shouldn't they? And they should be using memory barriers where necessary, either directly or indirectly through other, lower level primitives.

If you still think you need a low-level implementation, I know of no classes or libraries that implement barriers, but there's some implementation-specific code in the Linux kernel. Search for mb(), rmb() or wmb() in include/asm-{arch}/system.h.

aib
Hi Aib! I am not able to tell you right now why these are necessary. I am reading about "avoiding reordering" issue and I see code that doesn't use any lock at all..I am having a look here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html as starting point,,
Ah, I see. They're trying to avoid making the whole method synchronized (which is the simpler, albeit slower, solution).
aib
Note: "Changing the semantics to require releasing a lock to be a full memory barrier would have performance penalties." Another simpler, slower solution here. As far as I can tell (I'm too tired, sorry) their proposed solution separates the barriers from the locks to increase performance. I think you're going to need a low level memory barrier (i.e. a single, unportable instruction) here.
aib
+2  A: 

There are no low-level memory barriers in boost yet, but there is a proposed boost.atomic library that provides them.

Compilers provide their own either as intrinsics or library functions, such as gcc's __sync_synchronize() or _mm_mfence() for Visual Studio.

The C++0x library provides atomic operations including memory fences in the form of std::atomic_thread_fence. Though gcc has supplied various forms of the C++0x atomics since V4.4, neither V4.4 or V4.5 include this form of fence. My (commercial) just::thread library provides a full implementation of C++0x atomics, including fences for g++ 4.3 and 4.4, and Microsoft Visual Studio 2005, 2008 and 2010.

Anthony Williams
Hi! THanks.Proposed? If I am not wrong the boost.atomic you mean are in boost/interprocess/details so in the interprocess library. I am going to check...
Boost.Atomic is a new library, not a subset of Boost.Interprocess. The intention of boost.atomic is to implement the full set of C++0x-style atomic types, including fences and the `atomic<T>` template. Boost.Interprocess and some other boost libraries define specific atomic operations on specific types that do what they need and nothing else.
Anthony Williams
Thank you very much!