Fri, September 21, 2007, 09:27 AM under
dotNET |
ParallelComputing
Angelos
found an interesting article via the UK
MSDN Flash. If you did as well, check out more about that on the
author's blog (
one more reason to be sorry PDC was cancelled).
Anyway...
In a not so unrelated area, on a list that I am a member of, a question came up about affinitising a managed thread to a specific CPU. Jeffrey Richter came to the rescue by pointing out the
System.Diagnostics.ProcessThread
and its
ProcessorAffinity
property but then the harder question came along of how to associate that class with the
System.Threading.Thread
class. Below is the answer in C# from Mr Richter again:
// Call this passing in 0
public static ProcessThread GetProcessThreadFromWin32ThreadId(Int32 threadId) {
if (threadId == 0) threadId = ThreadUtility.GetCurrentWin32ThreadId();
foreach (Process process in Process.GetProcesses()) {
foreach (ProcessThread processThread in process.Threads) {
if (processThread.Id == threadId) return processThread;
}
}
throw new InvalidOperationException("No thread matching specified thread Id was found.");
}
...where
ThreadUtility
is a class containing at least:
[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
public static extern Int32 GetCurrentWin32ThreadId();
The code (and much more) is part the
Power Threading Library which is available on the
Wintellect site.