gpt4 book ai didi

c# - 线程.BeginThreadAffinity

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:08 24 4
gpt4 key购买 nike

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

Notifies a host that managed code is about to execute instructions that depend on the identity of the current physical operating system thread.

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

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

最佳答案

根据 msdn你是对的

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.

关于c# - 线程.BeginThreadAffinity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11833120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com