How do I find the processor on which my thread is running in C#?

给你一囗甜甜゛ 提交于 2020-01-01 12:27:09

问题


How do I find the processor on which my thread is running in C#?


回答1:


This is not necessarily a constant - the thread could be scheduled onto different cores across its lifetime. You can set affinity masks to tie a particular thread to a particular CPU if you want to. See the API docs for Thread.BeginThreadAffinity for more details on what can be done within .Net.




回答2:


IMHO it's possible that the .NET thread is not bound to any of the native threads. >NET runtime can move the .NET threads between different native threads and processors anytime.




回答3:


I'm not sure that you can. You can get the process affinity mask (GetProcessAffinityMask), and set the same (SetProcessAffinityMask). You can also set the thread affinity mask, but my understanding is that in doing so you restrict the thread to running on one of the processors you've set your affinity mask to.

If you're delving into specific threads running on specific cores you probably want to set the process affinity mask to define the set of cores your code can run on, and the threads in your process will then float among the selected cores.



来源:https://stackoverflow.com/questions/2340727/how-do-i-find-the-processor-on-which-my-thread-is-running-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!