A good example of this is the need to lock the timing thread to one core when dealing with the Windows timer on a multi-core machine. This is a well-known solution to the problem of erratic timing results returned by QueryPerformanceCounter. If you are using a library like SDL, which uses QPC under the hood for its timing calls, this is something you ought to consider. Unfortunately, Phobos is currently missing declarations for a couple of function calls and one type that you need to lock the timing thread down.
For many cases, this can be overcome by adding the appropriate declarations where you need them. In other cases, you'll find (particularly when using DMD) that the Win32 import libraries that ship with the compiler are outdated. In that case, you'll either need to generate them yourself or load the function symbols manually via LoadLibrary and friends. Luckily, for setting the thread affinity the solution is easy. Here's a complete example, ripped right out of the module where I use it.
[source]
version(Windows)
{
private
{
import core.sys.windows.windows;
import std.windows.syserror;
// Declarations missing from the windows module.
alias size_t DWORD_PTR;
extern(Windows)
{
DWORD SetThreadAffinityMask(HANDLE,DWORD);
BOOL GetProcessAffinityMask(HANDLE,DWORD_PTR*,DWORD_PTR*);
}
void setThreadAffinity()
{
void doThrow(string msg)
{
auto err = GetLastError();
throw new Exception(msg ~ sysErrorString(err));
}
DWORD_PTR procMask, sysMask;
if(!GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask))
doThrow("GetProcessAffinityMask failure: ");
DWORD_PTR mask = 1;
if(mask & procMask)
{
if(!SetThreadAffinityMask(GetCurrentThread(), mask))
doThrow("SetThreadAffinityMask failure: ");
}
else
{
throw new Exception("Unexpected affinity mask mismatch.");
}
}
}
}
[/source]
Drop this into module or class scope and add the following to an init method somewhere in the same module (or another module if you move setThreadAffinity out of the private block).
[source]version(Windows) setThreadAffinity();[/source]
Notice also that I imported std.windows.syserror, which exposes the sysErrorString function. I'm sure I wasn't the only one who overlooked that module. After years of using D, I only noticed it recently. If you're going to be making Win32 API calls, it will come in handy.