[zeromq-dev] Messages and shared memory

Martin Sustrik sustrik at 250bpm.com
Thu Jan 26 02:17:02 CET 2012

Hi Nadav,

> I have two threads that share a common large data structure. Is it safe
> for the threads to coordinate exclusive usage of this shared data by
> sending messages through a ZeroMQ inproc socket? In other words, is it
> guaranteed that when thread B receives a message sent by thread A, the
> data written by thread A prior to sending that message is committed to
> memory?

This gets complex :)

Internally when message is passed from one thread to another there's 
only an atomic operation invoked (see src_atomic_ptr.hpp).

On x86 you get consistency because of its cache coherency algorithm.

If you use a loose memory model CPU like Itanium, the atomic operation 
is done via mutex which in turn executes appropriate memory barriers.

So, it should work unless you are using some very strange hardware...

If so, just define ZMQ_FORCE_MUTEX when compiling 0MQ and the memory 
barriers will be executed as needed.


More information about the zeromq-dev mailing list