多久做一个管理线程切换操作系统线程?线程、做一个、多久、操作系统

2023-09-03 16:57:27 作者:有勇气就会有奇迹

据我所知,托管线程不能保证相同的操作系统线程上运行。

I understand that managed threads are not guaranteed to run on the same OS thread.

如果在CLR可以切换操作系统线程之间的托管线程,多长时间会出现这种情况?频率有什么影响?

If the CLR may switch a managed thread between OS threads, how often does this happen? What influence the frequency?

我有一个separate 的问题,对如何发生停止开关。二等奖对我来说将是这不会发生太频繁(少于一次一分钟就可以了)。的

I've got a separate question on how to stop the switching from happening. Second prize for me would be for this to not happen too often (less than once a minute would be fine).

推荐答案

这完全取决于主机。谁也不能保证在何时或在一个线程切换可能发生(如果有的话),给予任何特定的主机。

It completely depends on the host. There is no guarantee as to when or where a thread switch might take place (if at all), given any particular host.

然而,由于.NET 2.0,你已经能够调用静态BeginThreadAffinity方法来通知运行在$ C $,c取决于底层操作系统线程的标识主机:

However, since .NET 2.0, you have been able to call the static BeginThreadAffinity method to notify the host that the code that is running depends on the identity of the underlying OS thread:

http://msdn.microsoft.com/en-us/library/system.threading.thread.beginthreadaffinity(VS.80).aspx

当然,一定要调用EndThreadAffinity方法,当你的线程完成(我不知道,如果你只是让线程结束,而无需调用EndThreadAffinity会发生什么。我无法想象它会产生影响,但它的更好的是明确在这个问题上,IMO):

Of course, make sure you call the EndThreadAffinity method when your thread is done (I'm not sure what happens if you just let the thread end without calling EndThreadAffinity. I can't imagine it would have an impact, but it's better to be explicit in this matter, IMO):

http://msdn.microsoft.com/en-us/library/system.threading.thread.endthreadaffinity(VS.80).aspx