[zeromq-dev] Threading questions

gonzalo diethelm gdiethelm at dcv.cl
Fri Feb 19 14:07:44 CET 2010


> > Got it. Do I have to protect access to 0MQ's context with a mutex
within
> > each thread?
> 
> You should access each socket from only one thread. Therefore there's
no
> need for synchronisation.

Yes, but the question is regarding 0MQ's _context_. When I create a
socket within a thread, I pass the context as a parameter; should that
be protected with a mutex?

> Actually, that's the goal of the whole thing, having to do lock/unlock
> on each call to 0MQ would decrease performance by an order of
magnitude.

I see how you can get away with not synchronizing calls to send / recv /
etc., since each only needs access to the socket, which is per-thread.
This is great for performance.

> > Then the answer would be yes? I said "more than one thread calling
> > zmq_recv() on DIFFERENT sockets at the same time".
> 
> Hm. One thread cannot do two things at the same time. So it can access
> at most one socket at a time.

Just to clarify. I knew you are not supposed to call recv() on the SAME
socket from DIFFERENT threads. Now it is clear you CAN call recv() on
DIFFERENT sockets from DIFFERENT threads (and without using any mutex).

> >> 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?
> 
> There are two sockets there, but I've named both of them 's' which
made
> the example confusing. Have a look at it now. The socket owned by the
> main thread is called 'sm', the one owned by the worker thread is
called
> 'sw'.

Yes, you are right. I meant "there are two sockets and one communication
channel". But yes, there are two sockets there.

> > 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.
> 
> Yes, they are.
> 
> You have statements like this in zmq_socket(3):
> 
> ZMQ_PUB - Socket to distribute data. Recv fuction is not implemented
for
> this socket type.
> 
> Which implies the uni-directionality, but you are right, it should be
> made more explicit.

Which brings me to a sort of a more generic request: I have been missing
all along a more high-level description of 0MQ (2.0 particularly):
architecture, design goals, types of sockets, use cases, etc. You can
certainly build a picture from all the scattered documents, but they are
scattered, some are for 1.0 and some things are not really spelled out
anywhere. It would be really nice if the About section on the web site
gave more high-level details about ZMQ.

Again, this is not to say anything bad about the project. In my case,
ZMQ matches so closely with what I have been looking for, it is almost
uncanny. 

> Martin

-- 
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