[zeromq-dev] socket passing and thread safety.. a precise exlanation

Pieter Hintjens ph at imatix.com
Fri Oct 19 05:49:55 CEST 2012


On Fri, Oct 19, 2012 at 8:16 AM, Apostolis Xekoukoulotakis
<xekoukou at gmail.com> wrote:

> I would like to know when it is possible to pass a socket without creating
> problems?

You're using CZMQ, I assume...?

You can pass a socket from one thread to another if you do not use it
in the original thread after you create the child thread (that acts as
a full memory barrier). The child thread must close it.

Are you sure you can't create the socket in the child thread where it
belongs? You can use a separate context and a known TCP endpoint.

> I create worker threads with zthread_fork (czmq) and I want that the pub
> socket send messages to workers. Will this work? How can it work?

Workers create SUB sockets and connect them to a tcp:// endpoint that
is a PUB socket; whether it's another thread, process, or box, doesn't
matter.

If you want to use an inproc transport then you must ensure all
threads get the same context. However you can still create the PUB
socket in the thread that uses it, and then you must create it and
bind it before you start the worker threads.

> I would also like to point out that zthread manual contains information that
> is not self complete, the missing parts are not contained in the guide. It
> requires either knowledge of the internals of zeromq or (on threading in
> general?).

Could you be more specific? I'll expand the zthread manual as needed.

-Pieter



More information about the zeromq-dev mailing list