应该“结合” “纺丝”线程到某个核心? [英] should I "bind" "spinning" thread to the certain core?

查看:161
本文介绍了应该“结合” “纺丝”线程到某个核心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序包含几个旋转的延迟关键线程,即从不阻止。
这样的线程希望占用一个CPU内核的100%。然而,现代操作系统经常将线程从一个核心传递到另一个核心。例如,使用这个Windows代码:

My application contains several latency-critical threads that "spin", i.e. never blocks. Such thread expected to take 100% of one CPU core. However it seems modern operation systems often transfer threads from one core to another. So, for example, with this Windows code:

void Processor::ConnectionThread()
{
    while (work)
    {
        Iterate();
    }
}

我看不到100%任务管理器,整个系统负载为36-40%。

I do not see "100% occupied" core in Task manager, overall system load is 36-40%.

但如果我将其更改为:

void Processor::ConnectionThread()
{
    SetThreadAffinityMask(GetCurrentThread(), 2);
    while (work)
    {
        Iterate();
    }
}

然后我看到其中一个CPU内核100%占用,也将整个系统负载降低到34-36%。

Then I do see that one of the CPU cores is 100% occupied, also overall system load is reduced to 34-36%.

这是否意味着我应该倾向于 SetThreadAffinityMask spin线程?如果我改善了延迟添加 SetThreadAffinityMask 在这种情况下?我还应该为自旋线程做什么以提高延迟吗?

Does it mean that I should tend to SetThreadAffinityMask for "spin" threads? If I improved latency adding SetThreadAffinityMask in this case? What else should I do for "spin" threads to improve latency?

我正在将我的应用程序移植到Linux,所以这个问题更多关于Linux这很重要。

I'm in the middle of porting my application to Linux, so this question is more about Linux if this matters.

upd 找到此幻灯片,显示绑定忙等待线程到CPU可能有帮助:

upd found this slide which shows that binding busy-waiting thread to CPU may help:

推荐答案

在大多数情况下,如果这是代码中最重要的事情,运行锁定到单个核心的线程会为该线程提供最佳延迟。

Running a thread locked to a single core gives the best latency for that thread in most circumstances if this is the most important thing in your code.

原因(R)是


  • 您的代码可能在您的iCache

  • 分支预测变量已调整到您的代码

  • 您的数据可能已在您的dCache中准备

  • TLB指向您的代码和数据。

  • your code is likely to be in your iCache
  • the branch predictors are tuned to your code
  • your data is likely to be ready in your dCache
  • the TLB points to your code and data.

除非

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