[zeromq-dev] zmq multithread bug

Martin Sustrik sustrik at 250bpm.com
Wed Jan 27 08:56:57 CET 2010


Hi Guo,

> Hi All, the sample code below publishes data into socket from two 
> thread: One thread publishes "dummy", the other one publishes "from 
> server: 1". But the data is not properly received on the other side, 
> this is the print out:

The sockets are strictly non-thread-safe. There's no locking involved on 
socket operations what makes 0mq as fast as it is. Therefore you 
shouldn't write to a single socket from 2 threads.

If what you need is delivering messages from N sources to M destinations 
via TCP you can either:

1. Explicitly connect from each receiver to each sender or vice versa. 
Unless you have a stable set of client this would be a management nightmare.

2. Use zmq_forwarder device. All the senders will connect and send 
messages to the device. All the receivers will connect and read messages 
from the device. See examples/chat example.

3. Use multicast bus, either PGM or UDP. However, multicast has its own 
set of drawbacks, so it not absolutely needed for perf reasons, go 
rather with option 1 or 2.

Martin



More information about the zeromq-dev mailing list