[zeromq-dev] connect from a different thread

Martin Sustrik sustrik at 250bpm.com
Sun Jan 2 12:42:01 CET 2011

Hi Gaspard,

> I have a zmq.SUB socket handling remote messages in thread A. It spends
> most of its time in recv().
> Some other thread B running mDNS finds services and I want it to connect
> the subscriber (created in thread A) to the remote end.
> Is it ok to connect from a different thread ?

No. You cannot access one socket from multiple threads. In 2.1 you can 
migrate a socket to another thread, however, you still can't access it 
from multiple threads in parallel.
> Is it ok to connect while the socket is in recv() ?

No. As explained above, a socket is not thread-safe.

> If not, one solution I see would be to connect the subscriber to an ipc
> socket (created in the mDNS loop) to receive connection info. Is this
> the way to handle such a pattern ?

Yes. You can create another socket to pass connection info's to the 
subscriber thread. The subscriber thread then has to wait using 
zmq_poll, polling on both sockets, instead of simply calling recv.


More information about the zeromq-dev mailing list