[zeromq-dev] Communication between two threads

Martin Sustrik sustrik at 250bpm.com
Fri Mar 19 14:47:33 CET 2010


Hi Gonzalo,

>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've got lost in the scenario :) Can you write down few points showing
step-by-step what your application should do?

For inter-thread communication you should definitely use inproc
transport. Messing with synchronisation primitives, you would introduce
a bottleneck to the system.

Not sure whether it's relevant, but there's an example of how to create
in inproc communication channel here:

http://www.zeromq.org/area:docs-v20#toc11

Martin




More information about the zeromq-dev mailing list