[zeromq-dev] libzmq and TLS (thread local static) ?

Goswin von Brederlow goswin-v-b at web.de
Sat Feb 1 13:00:44 CET 2014


On Wed, Jan 29, 2014 at 02:39:46PM +0100, Laurent Alebarde wrote:
> Hi Devs,
> 
> I assume every function in the libzmq API is thread safe, but there
> are static variables in the code and no use of __thread ? Are all
> static variables defined in libzmq global to all threads ?
> 
> If I add a static variable which need to be thread local, say inside
> zmq::proxy, is it all right to add something like this in zmq.h ?
> 
> #if (__cplusplus > 199711L) || (defined(__STDCXX_VERSION__) &&
> (__STDCXX_VERSION__ >= 201001L))
> #define _TTHREAD_CPP0X_
> #endif
> 
> #if !defined(_TTHREAD_CPP0X_) && !defined(thread_local)
> #if defined(__GNUC__) || defined(__INTEL_COMPILER) ||
> defined(__SUNPRO_CC) || defined(__IBMCPP__)
> #define thread_local __thread
> #else
> #define thread_local __declspec(thread)
> #endif
> #endif
> 
> Found here: https://github.com/DFHack/dfhack/blob/master/depends/tthread/tinythread.h
> About __thread and thread_local:
> http://stackoverflow.com/questions/7047226/using-thread-in-c0x
> 
> Usage:
> 
> static __thread char *p;
> 
> Cheers,
> 
> 
> Laurent

As a side node: zmq is not thread safe, only parts of it are. Never
share sockets between threads for example. I would assume any thread
related function not to be thread safe, as in you can't call them from
two threads with the same socket. I bet message functions are also not
made thread save.

It goes with the "just don't share state" philosophy.

MfG
	Goswin



More information about the zeromq-dev mailing list