Mas*_*oud 3 c# cpu multithreading
我有 2 个线程。我想告诉其中一个在第一个 cpu 上运行,第二个在第二个 cpu 上运行,例如在一台有两个 cpu 的机器上。我怎样才能做到这一点?
这是我的代码
UCI UCIMain = new UCI();
Thread UCIThread = new Thread(new ThreadStart(UCIMain.main));
UCIThread.Priority = ThreadPriority.BelowNormal;
UCIThread.Start();
Run Code Online (Sandbox Code Playgroud)
并且该类肯定UCI有一个名为main. 例如,我想在第一个处理器中设置这个线程
我不会推荐这个,但如果你真的需要:它可以通过利用本机 Win32 系统调用,特别是SetThreadAffinityMask. 你需要做一些事情DllImport:
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
[DllImport("kernel32.dll")]
static extern IntPtr SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask);
Run Code Online (Sandbox Code Playgroud)
然后在每个生成的线程中使用它们(当然,掩码使用不同的参数):
// set affinity of current thread to the given cpuID
SetThreadAffinityMask(GetCurrentThread(), new IntPtr(1)); // CPU 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
916 次 |
| 最近记录: |