[zeromq-dev] Sharing socket safely between threads

Luca Boccassi luca.boccassi at gmail.com
Tue May 8 11:19:58 CEST 2018


On Mon, 2018-05-07 at 13:42 +0000, Attila Magyari wrote:
> Hello,
> 
> I know that zmq sockets are not thread safe. However I can't think of
> a
> good design (explained below) without using the socket in several
> threads.
> What I want to do is connect the socket in a thread, and then use it
> in a
> different one until the end, and never in parallel between several
> threads.
> I will guard the access with a mutex as well. Do you think this will
> be
> safe?

No - the chances of getting it wrong are very high and you'll end up
with weird race conditions, hard to debug. Better to spend the time up-
front to avoid the need for that.

> Just in case someone might have a better idea for my design, here is
> what
> I'm trying to do:
> I have an application which starts another process, passes a port to
> the
> newly created process, so it can connect back to the main application
> through a ZMQ_PAIR socket. Both processes are local to the machine.
> The
> main application will request from the second one, but the second one
> can
> initiate messages without an explicit request as well. Do you have a
> nicer
> design to achieve something like this?
> 
> Thank you in advance!

Use inproc/pair sockets - they are made and used extensively for inter-
thread communication.

-- 
Kind regards,
Luca Boccassi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20180508/521433a8/attachment.sig>


More information about the zeromq-dev mailing list