sched_yield减慢其他线程的速度 [英] sched_yield slow down other threads

查看:192
本文介绍了sched_yield减慢其他线程的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有在循环内使用sched_yield的代码.当我们这样做时,我们似乎会发现其他线程的性能降低,特别是那些涉及内核调用的线程(例如IO和互斥锁/事件处理).我正在尝试确定此行为的确切原因.

We have code that makes use of sched_yield inside a loop. When we do this we seem to get a slower performance of other threads, in particular those involving kernel calls (like IO and mutex/event handling). I'm trying to determine the exact cause of this behaviour.

sched_yield的过多调用是否会导致内核瓶颈?

Can excessive calls to sched_yield lead to a bottleneck in the kernel?

我的怀疑是,如果我们继续要求内核检查其进程列表,则其他线程将受到损害,因为关键数据结构可能会不断被锁定-而如果我们不调用sched_yield,那么这些内核锁定将趋于毫无争议. .这有意义吗,还是完全可以重复调用sched_yield.

My suspicion is if we keep asking the kernel to check its process list then other threads will suffer as key data structures may be continually locked -- whereas if we didn't call sched_yield those kernel locks would tend to be uncontested. Does this make sense, or should it be totally okay to repeatedly call sched_yield.

推荐答案

看看

Have a look at the sched_yield man page for Linux:

避免调用sched_yield() 不必要或不适当地(例如,当其他人需要资源时) 可调度线程仍由调用方保留),因为这样做会导致 在不必要的上下文切换中,这会降低系统性能.

Avoid calling sched_yield() unnecessarily or inappropriately (e.g., when resources needed by other schedulable threads are still held by the caller), since doing so will result in unnecessary context switches, which will degrade system performance.

在紧密循环中调用它会导致问题.降低通话速率.

Calling it in a tight loop will cause problems. Reduce the rate at which you're calling it.

(并检查您是否首先需要调用它.调度程序通常自己完成Right Thing.)

(And check that you need to call it in the first place. The scheduler often does the Right Thing all by itself.)

如果您的线程优先级低,您可能会发现其他有趣的选择:

Other options you could find interesting to investigate if you have a low priority thread:

  • sched_setscheduler - with SCHED_IDLE or SCHED_BATCH maybe (affects the whole process)
  • thread_setschedparam - per thread, but might have restrictions on what policies you can use (can't find it right now).

或者很好的旧版 nice 当然是命令.

Or the good old nice command of course.

这篇关于sched_yield减慢其他线程的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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