简体   繁体   English

如何强制CLR进行OS线程切换?

[英]How do I force the CLR to exhibit OS thread switching?

Related to these questions: 与这些问题有关:

How do I get the _real_ thread id in a CLR "friendly" way? 如何以CLR“友好”方式获取_real_线程ID?

How often does a managed thread switch OS threads? 受管线程多久切换一次OS线程?

I would like to be able to actually test the Thread.BeginThreadAffinity() methods and verify how they work and that they work. 我希望能够实际测试Thread.BeginThreadAffinity()方法并验证它们如何工作以及它们是否工作。

Is there some .NET functionality that will force an OS thread switch? 是否有一些.NET功能会强制执行OS线程切换?

There is not much to test with Thread.BeginThreadAffinity(). 使用Thread.BeginThreadAffinity()无需太多测试。 I calls a function in the CLR host, IHostTaskManager::BeginThreadAffinity() . 我在CLR主机中调用IHostTaskManager :: BeginThreadAffinity()函数。 IHostTaskManager is an optional interface that a custom CLR host can implement to provide a custom thread implementation, one that doesn't necessarily use an operating system thread. IHostTaskManager是一个可选接口,自定义CLR主机可以实现该接口以提供自定义线程实现,该接口不一定使用操作系统线程。 The ICLRTaskManager and ICLRTask interfaces provide the core services for such a custom thread. ICLRTaskManager和ICLRTask接口为此类自定义线程提供了核心服务。

These interfaces were added in .NET 2.0, on request by the SQL Server team. 这些接口是应SQL Server团队的要求在.NET 2.0中添加的。 SQL Server has had a custom threading option built in for a long time, based on fibers . SQL Server基于纤维内置了很长时间的自定义线程选项。 Fibers were popular in the olden days when machines with multiple processor cores were still rare. 光纤在过去很流行,当时带有多个处理器内核的机器仍然很少。 Other names for a fiber are "green thread" and "co-routine". 光纤的其他名称是“绿线”和“常规”。 They've been put to pasture by the multi-core revolution in the previous decade. 在过去十年中,多核革命已将它们放牧了。

The SQL Server project was a bust. SQL Server项目很失败。 They could not get it reliable enough and abandoned the project. 他们无法获得足够的可靠性,因此放弃了该项目。 Unfortunately we are left with the consequences, there is no simple way to map a .NET thread to an OS thread, the subject of your first link. 不幸的是,后果不堪设想,没有简单的方法可以将.NET线程映射到OS线程,这是您的第一个链接的主题。 As well as the considerable FUD shown in the accepted answer. 以及已接受答案中显示的大量FUD。

While the CLR still has the basic support for this feature, I do not know of a single example where a custom host implements its own threading. 虽然CLR仍对此功能提供基本支持,但我不知道一个示例,其中自定义主机实现了自己的线程。 The massive failure of the SQL Server team project certainly was a major signpost that this is difficult to implement, considering the resources the team had access to to make this work. 考虑到团队可以用来完成这项工作的资源,SQL Server团队项目的巨大失败无疑是一个很难实现的主要标志。 And it just doesn't make sense in general, mapping a single thread to a single processor core, as done by the operating system by default and used by the default CLR host, is incredibly hard to beat for efficiency. 而且这通常没有任何意义,将单个线程映射到单个处理器核心(如默认情况下由操作系统完成并由默认CLR主机使用),在效率方面难以超越。 Processor cores are very cheap to buy these days. 如今,处理器内核的价格非常便宜。

Long story short: Thread.BeginThreadAffinity() does nothing . 长话短说:Thread.BeginThreadAffinity()不执行任何操作 CLR threads are already affine to OS threads by default. 默认情况下,CLR线程已经类似于OS线程。 The odds that you'll ever run into a custom CLR host where it does anything at all are sufficiently close to zero to ignore the method. 您将在自定义CLR主机中执行任何操作的几率非常接近零,从而忽略了该方法。

A simple way to invoke an OS thread context switch is by using one of the WaitHandle.WaitXxx() methods or Thread.Sleep() with a non-zero wait. 调用OS线程上下文切换的一种简单方法是使用WaitHandle.WaitXxx()方法或Thread.Sleep()中的一种,且等待时间为非零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM