[zeromq-dev] Threading questions

gonzalo diethelm gdiethelm at dcv.cl
Fri Feb 19 13:43:56 CET 2010

> > Say I want to have threading operations on three separate threads.
Do I

Sorry, I meant "have socket operations on three separate threads".

> > have to call zmq_init() just once, with app_threads = 3, before I
> > the threads? Or do I have to call zmq_init() once on each thread,
> > time with app_threads = 3?
> Call it once in the main thread. Use the resulting context to create
> sockets in each of your threads.

Got it. Do I have to protect access to 0MQ's context with a mutex within
each thread?

> > Can I have more than one thread calling zmq_recv() on different
> > at the same time?
> No. Each socket object can be created and used from at most one
> For communication between threads use inproc transport (zmq_inproc(7))

Then the answer would be yes? I said "more than one thread calling
zmq_recv() on DIFFERENT sockets at the same time".

> See an example here:
> http://www.zeromq.org/area:docs-v20#toc15

The example only shows one inproc socket being used to communicate
between two threads. I assume one of the threads can then go on to
create other socket(s), right?

> > I want to have a program that receives a constant stream of business
> > messages and, every once in a while, a couple of "administrative"
> > messages (enquiring state, asking it to pause or shut down, etc.). I
> > think I have at least two ways of doing this:
> >
> >    1. One socket, one thread receiving messages, it decides what
kind of
> >       message it is (business or administrative) and processes it.
> >    2. Two sockets, two threads. On one thread I only receive
> >       messages on the business socket; on the other thread I only
> >       receive administrative messages on the administrative socket.
> > Is there any advantage you can see on one method over the other?
Model 2
> > seems to provide a better abstraction.
> As far as I understand your business messages are a stream (either
> pub/sub or upstream/downstream) while your administration commands are
> req/rep. You'll need 2 sockets for that.

You are right. Which makes me wonder: are pub/sub and
upstream/downstream sockets inherently one-way? I don't remember seeing
any explicit statement to this effect in the docs.

> > Are all of the threading rules the same when using 0mq on C / C++
> > when using it on Java?
> Yes. It's the same. Java binding is just a thin wrapper over
> C library.

Got it.

> Martin

It does, a lot. Thanks.

Gonzalo Diethelm

Declaración de confidencialidad: Este Mensaje esta destinado para
el uso de la o las personas o entidades a quien ha sido dirigido y
puede contener información reservada y confidencial que no puede
ser divulgada, difundida, ni aprovechada en forma alguna. El uso no
autorizado de la información contenida en este correo podrá ser
sancionado de conformidad con la ley chilena. 
Si usted ha recibido este correo electrónico por error, le pedimos
eliminarlo junto con los archivos adjuntos y avisar inmediatamente
al remitente, respondiendo este mensaje. 

"Before printing this e-mail think if is really necesary".
Disclosure: This Message is to be used by the individual,
individuals or entities that it is addressed to and may include
private and confidential information that may not be disclosed,
made public nor used in any way at all. Unauthorized use of the
information in this electronic mail message may be subject to the
penalties set forth by Chilean law. 
If you have received this electronic mail message in error, we ask
you to destroy the message and its attached file(s) and to
immediately notify the sender by answering this message. 

More information about the zeromq-dev mailing list