[zeromq-dev] ZeroMQ thread safety (or not)
Alex Bligh
alex at alex.org.uk
Fri Feb 12 13:47:27 CET 2016
I am attempting to write a Go Channel wrapper (similar to vaughan0's work but working on top of the pebbe's go bindings), as I'm using zmq in an environment where I will often want to use a golang 'select' mixing zmq sockets and other golang channels. I have a question about thread safety.
The ZeroMQ guide says: "Remember: Do not use or close sockets except in the thread that created them.". I want to know whether this is in fact a prohibition.
In Vaughan0's GoLang bindings, the MakePair function creates a pair of PAIR sockets in one thread:
https://github.com/vaughan0/go-zmq/blob/master/util.go#L24
Each end of this pair is then passed to a different goroutine (thread-ish) here:
https://github.com/vaughan0/go-zmq/blob/master/channels.go#L34
(i.e. ProcessOutgoing uses outsock, ProcessSockets uses insock)
This is a pretty useful thing to do. Is it permissible?
Specifically is the following pattern permissible?
Thread 1 Thread 2
======== ========
Create socket
Connect() / Bind()
Pass details to thread 2
Never access socket again
Use socket to read/write
Close socket
This is essentially what the vaughan0 golang bindings are doing.
I understand completely that using the same socket in two threads at once is NOT permissible. However, unless the sockets are using thread local variables (which is going to be problematic in go as the same goroutine can be scheduled between different OS threads) I can't immediately see why the above would not work.
--
Alex Bligh
More information about the zeromq-dev
mailing list