[zeromq-dev] Sockets, Threads and Pooling

Martin Sustrik sustrik at 250bpm.com
Thu Aug 25 20:22:46 CEST 2011


Hi Brad,

> I've been trying to find out if its okay to use a socket across
> different threads. I understand that i can't use them at the same time
> across different threads, but I would implement this using some sort of
> connection pool. So a single socket would be used in a single thread at
> any given time, but that socket would be used in multiple threads in its
> lifetime.

Yes, you can do that. The only requirement is that full memory barrier 
is executed each time the socket is moved from one thread to another.

If the movement between threads is performed by the framework you are 
working in (such as you language's virtual machine) it executes the 
memory barriers for you.

If you are on your own (say the program is in C) you can for example 
place the socket to the critical section. In such case the barriers are 
executed when locking and unlocking the mutex.

Martin



More information about the zeromq-dev mailing list