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

Thiago Cangussu cangussu at gmail.com
Thu Jul 5 14:13:29 CEST 2012


On Thu, Jul 5, 2012 at 3:27 AM, Ivan Pechorin <ivan.pechorin at gmail.com>wrote:

>
>
> 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."
>

My bad, I only checked the guide/man-pages and was a little unsure that a
mutex would do the trick.



>  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.
>

That's the case, I was ok with the performance penalty of creating/closing
the socket, but the file descriptor limit is a problem. The mutex
lock/unlock will not be an issue.

Thanks a lot,
Thiago
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120705/e03d9c1b/attachment.htm>


More information about the zeromq-dev mailing list