[zeromq-dev] Communication between two threads

gonzalo diethelm gdiethelm at dcv.cl
Fri Mar 19 00:28:19 CET 2010


I want to have all communications for my program done over 0MQ running
on a separate thread. This thread has an infinite loop calling poll on
all the sockets I want to read from, and calls different handlers for
incoming messages. The other thread does processing and stuff.

Now, one of the sockets in this poll set is the REQ end of a REQ/REP
pair. I create it within the poll thread, otherwise 0MQ will complain
when trying to poll it. The question now becomes: how do I write to it
from the processing thread?

The only sane answer I can think about is to add another socket to my
poll set, an UPSTREAM inproc socket, and create a DOWNSTREAM inproc
socket in the processing thread. Now, when I want to send something from
the processing thread over the REQ socket, I write that something on the
DOWNSTREAM inproc socket; the UPSTREAM inproc socket in the polling
thread receives the message and stuffs it into the REQ socket. Does this
sound reasonable, or is this overly complex and I am missing a simpler
solution?

Hhmmm... In fact, this idea might be the correct one because I will also
need to pass the reply that I will get on the REQ socket, back to the
processing thread; instead of having something like a condition variable
to synchronize those threads, I could transform the UPSTREAM/DOWNSTREAM
inproc socket pair into another REQ/REP inproc socket pair, and have the
processing thread simply wait for an answer after it writes its request.

I would appreciate your collective thoughts on this problem. Thanks in
advance.

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