Author Topic: Thread Local Storage  (Read 91082 times)

TA

  • Newbie
  • *
  • Posts: 20
    • View Profile
Thread Local Storage
« 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.

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: Thread Local Storage
« Reply #1 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.

TA

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Thread Local Storage
« Reply #2 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?
« Last Edit: February 03, 2011, 02:41:11 PM by TA »

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: Thread Local Storage
« Reply #3 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.

TA

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Thread Local Storage
« Reply #4 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?

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: Thread Local Storage
« Reply #5 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.

TA

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Thread Local Storage
« Reply #6 on: February 03, 2011, 03:06:58 PM »
Thanks.