Cannot get thread ID with Thread.getThis() in specific callback functions on Windows

Rainer Schuetze r.sagitario at gmx.de
Fri Dec 10 00:18:41 PST 2010


I tried your code with the additions, and it works (dmd-2.050 on XP/32).

If you are on some 64-bit system: I've seen threads without any TLS set 
up at all. If you check the disassembly at the crash location, and there 
is a read from FS:[0x2c] that results in 0, then you are probably 
hitting this problem.

It is rather delicate to modify the windows TLS data structures, so an 
option might be to not touch any TLS in the waveInProc (including any 
memory allocations), but to just set an event to notify another thread 
that has been created with "new Thread".

Rainer

Haruki Shigemori wrote:
> (2010/12/10 5:14), Rainer Schuetze wrote:
>>
>> A DLL gets notified in its entrypoint DllMain about new threads. When it
>> receives such a notification, it mainly does the following:
>>
>> if( !thread_findByAddr( GetCurrentThreadId() ) )
>> thread_attachThis();
>> if( !_moduleinfo_tlsdtors_i ) // avoid duplicate calls
>> _moduleTlsCtor();
>>
>> You might want to try inserting this snippet into your callback.
>>
>> Rainer
> 
> Thank you!
> 
> $ type a.d
> import win32.windows;
> import win32.mmsystem;
> import std.stdio;
> import core.thread;
> 
> extern (Windows)
> void waveInProc(in HWAVEIN handle, in uint message, in DWORD instance, 
> in DWORD param1, in DWORD param2)
> {
>     if( !thread_findByAddr( GetCurrentThreadId() ) )
>         thread_attachThis();
>     if( !_moduleinfo_tlsdtors_i ) // avoid duplicate calls
>         _moduleTlsCtor();
> 
>     int[] a;
>     int[] b = a.dup;
> }
> ....
> $ bud a /Ldetailedmap -g -full
> ....
> $ ddbg a
> -> r
> ....
> Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at 
> gc.gcx.Gcx.__invariant src\gc\gcx.d:1553 (0x0041e507) thread(6136)
> ->q
> 
> Ummm...?


More information about the Digitalmars-d mailing list