gpt4 book ai didi

multithreading - sched_yield 的行为

转载 作者:行者123 更新时间:2023-12-04 02:30:12 27 4
gpt4 key购买 nike

我对 sched_yield 有几个问题函数,因为我看到它没有按我的代码中的预期运行。很多时候,当我尝试通过调用 sched_yield 来让出它时,即使存在其他线程,我也会看到同一个线程一次又一次地运行。 .

另外如果我有多核,sched_yield yield 适用于在所有内核或仅一个内核上运行的线程。例如,我在核心 1 上运行线程 1、2 和 3,在核心 2 上运行线程 4、5 和 6,如果 sched_yield是从线程 2 调用的,会只被线程 1 和 3 替换,还是 1、3、4、5 和 6 都可以?我问这个是因为在.Net Thread.Yield只屈服于在同一核心/处理器上运行的线程。

最佳答案

http://www.kernel.org/doc/man-pages/online/pages/man2/sched_yield.2.html

sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.

If the calling thread is the only thread in the highest priority list at that time, it will continue to run after a call to sched_yield().



sched_yield 不是 .Net 调用,线程/进程模型与此不同。 Windows/.NET 的调度器与 Linux 的调度器不同。 Linux 甚至有几个可能的调度器。

因此,您对 sched_yield 的期望是错误的。

如果要控制线程的运行方式,可以将每个线程绑定(bind)到 CPU。然后,线程将仅在绑定(bind)的 CPU 上运行。如果你有多个线程,绑定(bind)到同一个 CPU,使用 sched_yield 5月 切换到另一个绑定(bind)到当前 CPU 并准备运行的线程。

如果每个线程都想做大量 CPU 密集型工作,那么每个 CPU 使用多个线程也是个坏主意。

如果您想完全控制线程的运行方式,您可以使用实时线程。 http://www.linuxjournal.com/magazine/real-time-linux-kernel-scheduler - SCHED_FIFO 和 SCHED_RR RT 策略。

关于multithreading - sched_yield 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6357080/

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