Pthread - time.h::sleep() 和 pthread.h::pthread_yield() 有什么区别?
我花了很长时间寻找有关 time.h::sleep() 和 pthread.h::pthread_yield() 之间差异的信息,但无法找到任何可靠的参考资料,因此我发布了这个问题。
time.h::sleep() 和 pthread.h::pthread_yield() 有什么区别?
更新:
我问的原因是因为我使用 sleep() 来 sleep() 每个单独的线程...并且当有 8 个线程与 4 个线程时,我的应用程序开始出现问题。 当我上网查看 sleep() 是否只影响每个线程时,我找不到任何好的参考说明 Sleep() 是否影响整个进程或 sleep() 只影响单个线程。
I spent a good long while looking for info on the differences between time.h::sleep() and pthread.h::pthread_yield() but was unable to find any solid reference material and so I am posting this question.
What is the difference between time.h::sleep() and pthread.h::pthread_yield()?
Update:
The reason I ask is because I was using sleep() to sleep() each individual thread... and my application started having issues when there was 8 threads vs 4 threads. When I went online to see if sleep() only affects each thread, I couldn't find any good reference stating whether Sleep() affects the entire process OR sleep() only affects the individual thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 pthread_yield:
从睡眠手册页:
如果您不想在线程中出现实时延迟,而只想允许其他线程完成其工作,那么 pthread_yield 比 sleep 更适合。
From pthread_yield:
From the sleep manpage:
If you don't want to have a real time delay in your threads and just want to allow other threads to do their work, then pthread_yield is better suited than sleep.
sleep() 会导致程序停止执行一段时间。 无论系统上发生什么情况,您的线程都不会再次启动,直到至少传递给 sleep() 的时间长度过去。 pthread_yield() 通知操作系统您的线程已完成工作,并且它可以将执行切换到另一个线程。 但是,如果当时没有更高优先级的线程需要做工作,您的线程可能会立即再次启动。
IOWs,在 sleep() 之后,即使没有其他线程需要运行,您的线程也保证会停止运行,而 pthread_yield() 只是一种礼貌的方式,让其他线程有机会在需要时运行。
更新响应问题更新: sleep() 和 pthread_yield() 仅影响调用线程。
sleep() causes your program to stop executing for a certain length of time. No matter what else happens on the system, your thread will not start again until at least the length of time passed to sleep() has elapsed. pthread_yield() notifies the operating system that your thread is done working, and that it can switch execution to another thread. However, if there is no higher-priority thread that needs to do work at that time, your thread may start again immediately.
IOWs, after sleep() your thread is guaranteed to stop running even if no one else needs to run, while pthread_yield() is just a polite way to give other threads a chance to run if they need to.
Update in response to question update: both sleep() and pthread_yield() affect only the calling thread.
sleep(s) 获取当前执行线程并将其挂起,直到 s 秒结束通过(或者它被信号唤醒。)
更实际地说,当您调用 sleep() 时,该线程将停止执行并只是...等待,直到指定的时间过去。 一旦通过,该线程就会被放入就绪队列中。
pthread_yield() 表示“获取此线程,并将其放入就绪队列”。 您的线程将停止执行并处于“等待”状态,以供调度程序选择/运行。 这并不能保证您的线程不会立即恢复运行。 但它为另一个线程提供了在其执行过程中的给定点运行的机会。
我要大胆地说 sleep(0) 会完成与 pthread_yield() 相同的事情 - 既停止执行并将线程放入就绪队列中。
sleep(s) takes the current thread of execution and suspends it until s seconds have passed (or it is woken up by a signal.)
In more practical terms, when you call sleep(), that thread will cease execution and just... wait until the specified time has passed. Once it passes, that thread is placed into the ready queue.
pthread_yield() says "take this thread, and put it into the ready queue." Your thread will stop execution and be in the 'waiting' state to be selected/run by the scheduler. This does not gaurantee that your thread will not immediately resume running. But it gives another thread a chance to run at a given point in its execution.
I am going to go out on a limb and say that sleep(0) would accomplish the same thing as a pthread_yield() - both stopping execution and placing the thread in the ready queue.