[zeromq-dev] Multithreaded ZeroMQ servers

Tom Quarendon tom.quarendon at teamwpc.co.uk
Mon Jan 4 12:48:57 CET 2016

I’m confused about writing multithreaded servers with ZeroMQ.
I have read the ZeroMQ guide and there’s a section “Multithreading with ZeroMQ” that has a description of how to write a multithreaded Hello World server.

However there appears to be an assumption that the “work” of each request takes essentially the same amount of time. That is, the requests are dealt out to the workers with a dealer socket, and so the requests are just dealt out round robin aren’t they? So if one request takes a long time, you could get into the situation that it ends up with lots of requests queued up behind it for that worker, and all the other workers are idle. That doesn’t seem right to me. But I may be misunderstanding how it works.

In my mind what you want to be able to do is somehow have all of the threads read from the same socket, or bind to the same external endpoint, so that the each time a worker does a recv it gets the next message that is in the input queue. Either that or you would appear to need each worker to disconnect from the inproc socket each time it has received a request to process so that it doesn’t get dealt any more, and then reconnect when it has finished, but that feels like it might be undesirably inefficient. Either that or I have to have a big lock and memory fence around the input socket and just have each thread block waiting for the mutex and then reading the next message.

What I seem to want to end up doing is writing my own simple thread safe in-process queue that I can just put the requests on to and have all of the worker threads read from. However I’d like to use ZeroMQ if I can, I just can’t work out how to.

What am I missing?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20160104/6df3813a/attachment.htm>

More information about the zeromq-dev mailing list