[zeromq-dev] [jzmq] xreq/xrep sockets and thread safety

Alexey Ermakov zee at technocore.ru
Thu Aug 12 19:09:33 CEST 2010


Hi everyone,

I'm trying to figure out how to deal with the thread safety restriction of
0MQ sockets. As I understand, they are bound to a thread, which means that I
write/read to the same socket from multiple threads (which is possible with
Java Posix socket wrappers). I'm trying to figure out how to implement a
rather simple task and I can't think of a decent solution.
My application is a Scala-based server that has an XREP socket, processes
the requests, and replies back. The catch is that request processing is
implemented by forwarding a request to another service using an asynchronous
API. How am I supposed to send the replies back, if the XREP socket is bound
to the thread that is basically blocked on "while (true) socket.recv()"? I
suppose I could do a 50ms poll on the socket and then process the replies
(fetching them from a LinkedBlockedQueue or something), but that would be
inefficient, because requests/replies happen much less frequently. Another
solution would be replacing all inter-thread communication with 0MQ sockets
and doing a poll() on these sockets in the request handler, but that doesn't
sound very nice.
Is there an easier solution I'm overlooking?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100812/166b9e98/attachment.htm>


More information about the zeromq-dev mailing list