Hi Michael, To properly understand thread safety in 0MQ, you need to understand at least how sockets are used for both inter-thread work, and inter-process work.
For inter-thread work, we build our threads as message processing tasks, where each thread loops on incoming messages. Each thread owns a set of sockets that provide it messages, and on which it sends outgoing messages to other threads. In this design it makes no sense to share sockets between threads; they are literally the endpoints inside the thread for message i/o. For inter-process work, it conceivably makes more sense (at least until you understand 0MQ) to consider sharing a socket between threads, since one incoming stream may need multiple worker threads. However the 0MQ answer is to use one thread as distributor, reading from a frontend socket and forwarding to worker threads on a backend socket. The Guide has examples of this. So "sharing sockets between threads" is really a non-question, it's only something people will try when they have not yet understood how to use 0MQ properly. The one exception to this general rule is in esoteric cases where you may create a socket in one thread, for use by an other thread. -Pieter On Mon, Apr 8, 2013 at 12:40 PM, Michael Powell <[email protected]> wrote: > Hello, > > I am still evaluating ZMQ as a potentially viable infrastructure for our > ecosystem messaging concerns. And now it seems threading is the topic of the > day, or rather the lack of thread-safety. This is partly ignorance on the > subject on my part, so correct me if I am wrong, or the paradigm seems > foreign as ZMQ was intended for use. > > I read that ZMQ Context is king. From what I can tell Context is > thread-safe. You can allocate new sockets and connect all day long on the > same context from different threads and you're fine, right as rain? Context, > context, context: context is king. > > Sockets themselves are not thread-safe. I'll hazard to guess allocating and > using sockets in a thread is a "trivial" matter. They are intended as > disposable objects. > > I am interested to design a pub/sub event brokering type system potentially > exposing ZMQ-based messaging throughout our ecosystem, inproc or IPC. If I > do that I need for my ZMQ adapters to handle context in a thread-safe > manner? Making the assumption publishers and subscribers maintain their own > sockets, etc. > > Regards, > > Michael Powell > > _______________________________________________ > zeromq-dev mailing list > [email protected] > http://lists.zeromq.org/mailman/listinfo/zeromq-dev > _______________________________________________ zeromq-dev mailing list [email protected] http://lists.zeromq.org/mailman/listinfo/zeromq-dev
