Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] Thread local storage optimizations
From: Anthony Williams (anthony_at_[hidden])
Date: 2009-01-24 06:11:27


At Sat 24 Jan 2009 06:09:15 UTC, Ioannis Papadopoulos
<ipapadop_at_[hidden]> wrote:

> Most compilers have support for thread local storage via a storage
> specifier (different in each compiler, but in the general case it is
> __thread).
>
> I have seen the implementation of thread and thread_specific_ptr and
> they both use the generic POSIX support. I know it's generic enough for
> all the systems, however if the compiler supports thread local storage,
> the latter is much faster than using what POSIX provides.

Possibly. However, it's not the same: POSIX allows a destructor
function to be registered, and very few compilers (any?) currently
allow types with destructors for thread-local variables.

> Is there any reason against using this facility, since it's gonna also
> be available through a new C++ keyword, thread_local (WG21 N2659
> proposal http://www.open-std.org/JTC1/sc22/wg21/docs/papers/2008/n2659.htm )

The new thread_local keyword supports types with destructors, and will
therefore be able to replace the use of TSS cleanup handlers.

It's also worth noting that even in compilers that support the use of
thread-local variables directly, this doesn't necessarily play well
with dynamic libraries. This is particularly a problem on Windows,
where DLLs loaded manually at runtime (as opposed to those loaded
automatically when the executable is started) do not have space for
their thread-local variables allocated by the system, which results in
memory corruption.

Anthony

-- 
Anthony Williams
Author of C++ Concurrency in Action | http://www.manning.com/williams
just::thread C++0x thread library   | http://www.stdthread.co.uk
Custom Software Development         | http://www.justsoftwaresolutions.co.uk
Just Software Solutions Ltd, Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk