[zeromq-dev] How to safely use a socket in multiple threads in zmq 2.1.11
Stephen Hemminger
shemminger at vyatta.com
Thu Jul 5 00:57:15 CEST 2012
On Wed, 4 Jul 2012 19:24:21 -0300
Thiago Cangussu <cangussu at gmail.com> wrote:
> Hi,
>
> I have a pool of threads that needs to send messages to a zmq socket. I was
> creating the socket, connecting, sending the message and closing it for
> each message, but it is for sure not an optimal usage of resources, since
> if I create/close sockets too fast, zmq is not able do release them as fast
> and I get the error: "too many open files (signaler.cpp 330)". Since I have
> no control over those threads, I'm about to try the following solution:
>
> 1 - create a pool of connected sockets (REQ, inproc)
> 2 - when a client thread wants to send a message, it grabs a socket from
> the pool
> 3 - the thread performs a send/recv
> 4 - the thread releases the socket (which will be again available in the
> pool)
>
> 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?
>
> Thanks,
> Thiago
You can have a per-thread pool of sockets, but you can't create socket
in one thread and then use it in another. Can you just have one socket
per thread in thread local storage?
More information about the zeromq-dev
mailing list