SethreadAffinityMask() 正确用法?
我有 1500 个线程..我希望它们在 12 个处理器上运行... 为此,我打电话 SetThreadAffinityMask(GetCurrentThread(),1<<(threadnum%numprocessors))
;其中处理器数 = 12。 面膜的正确使用方法是这样吗? 它需要可扩展,也就是说,如果我希望它只在 11 个处理器上运行,那么 SetThreadAffinityMask(GetCurrentThread(),1<<(threadnum%numprocessors));
其中 numprocessors=11。
I have 1500 threads..I want them to run on 12 processors...
To do that I callSetThreadAffinityMask(GetCurrentThread(),1<<(threadnum%numprocessors))
; where numprocessors=12.
Is that correct usage of the mask?
It needs to be scalable, that is if i want it to run on just 11 processors, thenSetThreadAffinityMask(GetCurrentThread(),1<<(threadnum%numprocessors));
where numprocessors=11.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正确的。
但出于性能和效率的原因,我建议您以某种方式更改线程模型,使线程数等于 CPU 执行核心的数量,然后这些线程处理由“任务管理器”分配的 1500 个任务/工作项。
如果您不想创建自己的“任务管理器”,可以使用 Windows ThreadPool API,您可以将任务分配给操作系统管理的“线程池”
that's correct.
but for performance and efficiency reasons, i'am suggesting that you change your threading model somehow that the thread count is equal to number of CPU execution core, then those threads work on your 1500 tasks/work item which distributed by your "task manager".
if you don't want to create your own "task manager", you can use windows ThreadPool API, which you assign a task to a "thread pool" managed by O/S
从语法上讲, SetThreadAffinityMask(GetCurrentThread(),1<<(GetThreadId()%numprocessors)) 是正确的,但仅仅因为有很多线程和处理器就使用亲和力并不是一个好主意。它会干扰调度程序并降低性能。您可以将其用于某些线程,以最大限度地减少缓存未命中。当线程从一个处理器移动到另一个处理器时,就会发生高速缓存未命中。
Syntactically SetThreadAffinityMask(GetCurrentThread(),1<<(GetThreadId()%numprocessors)) it's correct but it's not a good idea to use affinity just because you have a lot of threads and processors. it can interfere with the scheduler and degrade performance. You can use this for some threads to minimize cache misses. A cache miss occurs when a thread gets moved from one processor to another.