just::thread Support Forum

General Category => General Discussion about just::thread => Topic started by: TA on February 03, 2011, 02:20:45 PM

Title: Thread Local Storage
Post by: TA on February 03, 2011, 02:20:45 PM
Hello Anthony,

Thread Local Storage is added as a keyword to C++ standard and it's not possible to have it in library without any special support by compiler, however it would be useful to have a portable TLS in just::thread. I couldn't find anything like that in documentation and forum, so could you please tell me if there is TLS support in just::thread?

Thanks.
Title: Re: Thread Local Storage
Post by: Anthony Williams on February 03, 2011, 02:25:26 PM
There is currently no support for TLS in just::thread. Given that the compilers supported by just::thread do have TLS support, I will consider adding it for a future release.
Title: Re: Thread Local Storage
Post by: TA on February 03, 2011, 02:36:18 PM
There is currently no support for TLS in just::thread. Given that the compilers supported by just::thread do have TLS support, I will consider adding it for a future release.
Thanks for your answer Anthony.

Currently I'm using compilers support for TLS, but both GCC and MSVC have the same issue, they don't allow to have objects with non-trivial constructor in thread local storage, as far as I understood from C++ Concurrency in Action examples this is allowed in C++0x, maybe something like boost::thread_specific would be more useful here?
Title: Re: Thread Local Storage
Post by: Anthony Williams on February 03, 2011, 02:46:54 PM
Currently I'm using compilers support for TLS, but both GCC and MSVC have the same issue, they don't allow to have objects with non-trivial constructor in thread local storage, as far as I understood from C++ Concurrency in Action examples this is allowed in C++0x

Yes, in C++0x thread_local variables can be types with non-trivial constructors and destructors.

maybe something like boost::thread_specific would be more useful here?

Maybe. boost::thread_specific_ptr is slightly different again: though each pointer is freed automatically, you have to manually allocate and construct the object for each thread.
Title: Re: Thread Local Storage
Post by: TA on February 03, 2011, 02:53:11 PM
Maybe. boost::thread_specific_ptr is slightly different again: though each pointer is freed automatically, you have to manually allocate and construct the object for each thread.
It is, but at least it allows to have thread local objects and not to worry about freeing it up afterwards, which is simply impossible with __declspec(thread).
As far as I know boost::thread_specific is handled in special way in boost::thread calling procedure, so I can't use boost::thread_specific in std::thread, is it right?
Title: Re: Thread Local Storage
Post by: Anthony Williams on February 03, 2011, 03:02:40 PM
Maybe. boost::thread_specific_ptr is slightly different again: though each pointer is freed automatically, you have to manually allocate and construct the object for each thread.
It is, but at least it allows to have thread local objects and not to worry about freeing it up afterwards, which is simply impossible with __declspec(thread).
As far as I know boost::thread_specific is handled in special way in boost::thread calling procedure, so I can't use boost::thread_specific in std::thread, is it right?

You should be able to use boost::thread_specific_ptr alongside std::thread. boost::thread_specific_ptr provides automatic cleanup for non-boost threads for the compilers supported by just::thread. On Windows, you might have to use the DLL version of boost, though, since the static library might clash with the just::thread library for the automatic cleanup hook.
Title: Re: Thread Local Storage
Post by: TA on February 03, 2011, 03:06:58 PM
Thanks.