[PATCH] Destructor support for C++11 thread_local variables

Jason Merrill jason@redhat.com
Thu Oct 11 12:55:00 GMT 2012


On 10/11/2012 02:38 AM, Siddhesh Poyarekar wrote:
> On Wed, 10 Oct 2012 16:20:19 -0700 (PDT), Roland wrote:
>> I really hope there will be a way to use this from C, e.g.
>>
>> __thread type x;
>> static void __attribute__((thread_destructor (x))) foo (type *x_ptr)
>> { ... }

I don't think we want implicit destructor calls like this from C; in G++ 
__thread and thread_local behave differently because supporting dynamic 
initialization and destruction causes run-time overhead for all uses.

I think it would make sense to have a user-accessible thread_atexit 
entry point, however, so that people can explicitly register the 
destructor when they explicitly initialize the variable.

> The destructor does take an object pointer.  I could keep a static
> process-wide list of destructors instead of the current thread-local
> one and use locking to do this.  It won't work if the destructor
> depends on some context within its current thread.

Right, that's the problem.  In general we can't assume that reaching 
into another thread's local data will work sensibly.

> C++11x does not seem to say anything about thread_local behaviour when
> DSOs are involved.

Nope, C++11 doesn't say anything about DSOs at all.  So far it's been 
too hard to resolve the differences in semantics between ELF and PE into 
something useful.

Jason



More information about the Libc-alpha mailing list