多久做一个管理线程切换操作系统线程? [英] How often does a managed thread switch OS threads?

查看:275
本文介绍了多久做一个管理线程切换操作系统线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我有一个<一个href="http://stackoverflow.com/questions/1979191/enforcing-relationship-between-managed-thread-and-os-thread-cuda-usecase">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:

<一个href="http://msdn.microsoft.com/en-us/library/system.threading.thread.beginthreadaffinity%28VS.80%29.aspx">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):

<一个href="http://msdn.microsoft.com/en-us/library/system.threading.thread.endthreadaffinity%28VS.80%29.aspx">http://msdn.microsoft.com/en-us/library/system.threading.thread.endthreadaffinity(VS.80).aspx

这篇关于多久做一个管理线程切换操作系统线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆