Cannot get thread ID with Thread.getThis() in specific callback functions on Windows
Denis Koroskin
2korden at gmail.com
Sun Dec 5 23:59:04 PST 2010
On Mon, 06 Dec 2010 06:38:40 +0300, Haruki Shigemori
<rayerd.wiz at gmail.com> wrote:
> Hi.
> I cannot get thread ID with Thread.getThis() in specific callback
> functions on Windows.
> What is the cause of this problem?
>
> 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)
> {
> auto tid = Thread.getThis();
> assert(tid); // tid is null
> }
>
> void main()
> {
> WAVEFORMATEX formatEx;
> with (formatEx)
> {
> wFormatTag = WAVE_FORMAT_PCM;
> nChannels = 1;
> nSamplesPerSec = 44100;
> wBitsPerSample = 16;
> nBlockAlign = cast(ushort)(wBitsPerSample * nChannels /
> 8);
> nAvgBytesPerSec = nSamplesPerSec * nBlockAlign;
> }
>
> HWAVEIN handle;
> waveInOpen(&handle, WAVE_MAPPER, cast(WAVEFORMATEX*)&formatEx,
> cast(DWORD)&waveInProc, cast(DWORD)null, CALLBACK_FUNCTION);
>
> uint bufferSize = cast(uint)(formatEx.nAvgBytesPerSec *
> 1/+second+/);
> WAVEHDR* hdr = new WAVEHDR;
> hdr.lpData = cast(LPSTR)new ushort[bufferSize];
> hdr.dwBufferLength = bufferSize;
> hdr.dwFlags = 0;
> hdr.dwLoops = 0;
>
> waveInPrepareHeader(handle, hdr, WAVEHDR.sizeof);
> waveInAddBuffer(handle, hdr, WAVEHDR.sizeof);
> waveInStart(handle);
>
> readln();
> }
Because D runtime doesn't know about the newly created thread, and static
constructors weren't called. Try calling the following in your callbacks:
if (Thread.getThis() is null) thread_attachThis(); // if
(dont_know_about_this_thread) initialize_it();
More information about the Digitalmars-d
mailing list