2

我一直试图从我的一个程序中挤出更多性能,并且正在阅读优先级和亲和力设置,我遇到了Thread.BeginThreadAffinity,它的文档说:

通知主机托管代码即将执行取决于当前物理操作系统线程标识的指令。

我在我的程序中对此进行了测试,性能提高了大约 3-4%。据我了解,如果您的代码被移动到不同的物理操作系统线程,它会失去亲和性设置,但Thread.BeginAffinity它会保持在同一个线程上,保持亲和性设置,因为我的代码被提升了,我会获得更高的性能。

这是它的工作原理,还是我误解了Thread.BeginAffinity

4

1 回答 1

0

As per msdn you are correct

Some hosts of the common language runtime, such as Microsoft SQL Server 2005, provide their own thread management. A host that provides its own thread management can move an executing task from one physical operating system thread to another at any time. Most tasks are not affected by this switching. However, some tasks have thread affinity - that is, they depend on the identity of a physical operating system thread. These tasks must inform the host when they execute code that should not be switched.

For example, if your application calls a system API to acquire an operating system lock that has thread affinity, such as a Win32 CRITICAL_SECTION, you must call BeginThreadAffinity before acquiring the lock, and EndThreadAffinity after releasing the lock.

Using this method in code that runs under SQL Server 2005 requires the code to be run at the highest host protection level.

于 2012-08-06T18:02:35.567 回答