Reputation:
The documentation of ØMQ mentions:
Individual ØMQ sockets are not thread safe except in the case where full memory barriers are issued when migrating a socket from one thread to another.
What exactly is meant by "full memory barriers?" Can I have multiple threads send over the same ØMQ socket if I synchronize this with mutexes?
Upvotes: 0
Views: 851
Reputation: 1216
As Ulrich has said, yes you can synchronise access to a single thread using mutexes, but really, why would you want to do that?
It's normally considered good practice to only access a socket from a single thread, and synchronise between threads using messages. Something like this:
Worker thread 1
\
Worker thread 2 - > Control thread -> msg out
/
Worker thread 3
where only the control thread can send messages directly over the socket. Messages from the worker threads would be sent to the control thread over an inproc zmq socket that you would create. The control thread would process just one message at a time which avoids the need for the mutexes, provided the workers have no shared state.
Message based designs are easier to implement and debug, and much easier to maintain than designs using mutexes. If you can change the design to do that, I'd advise doing so.
Upvotes: 3
Reputation: 17444
Acquiring a mutex implies a memory barrier. This basically means that write operations must not be reordered in a way that they cross this operation. Summary: Yes, use a mutex to protect access to the ZMQ socket and you're fine.
Upvotes: 1