yield减慢其他线程的速度

yield减慢其他线程的速度

本文介绍了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.

推荐答案

看看 sched_yield 适用于Linux的手册页:

Have a look at the sched_yield man page for Linux:

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

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减慢其他线程的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:59