[zeromq-dev] How to safely use a socket in multiple threads in zmq 2.1.11

Ivan Pechorin ivan.pechorin at gmail.com
Thu Jul 5 08:27:15 CEST 2012


2012/7/5 Thiago Cangussu <cangussu at gmail.com>

>
> The question is: what should I do to make this multi-threaded usage of
> sockets safe? Is it enough to guarantee that multiple threads do not
> perform send/receive simultaneously?
>
>
This is already in the FAQ [1]:

"For those situations where a dedicated socket per thread is infeasible, a
socket may be shared *if and only if *each thread executes a full memory
barrier before accessing the socket. Most languages support a Mutex or
Spinlock which will execute the full memory barrier on your behalf."

In particular, for Linux/UNIX systems with pthreads, the memory barrier by
pthread_mutex_lock(), pthread_mutex_unlock() and a bunch of other pthread_*
functions is guaranteed by the POSIX specification [2].

So, if you don't have control on a number of threads that use ZeroMQ
sockets and the performance penalty of mutex lock/unlock on every operation
is not a problem for your needs, just make sure that your pool of sockets
is protected by mutexes.

[1] http://www.zeromq.org/area:faq
[2]
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120705/67e94ea6/attachment.htm>


More information about the zeromq-dev mailing list