简体   繁体   English

std :: this_thread :: sleep_for-上下文切换到此线程吗?

[英]std::this_thread::sleep_for - is context switch to this thread?

If I call in my main function std::this_thread::sleep_for(std::chrono::seconds(1)) and I create 2 threads (using std::thread ) and I have 1 cpu core. 如果我在主函数std::this_thread::sleep_for(std::chrono::seconds(1))调用,我创建了2个线程(使用std::thread ),并且我有1个cpu核心。

Are there any context switching to main thread during this time? 在这段时间内是否有任何上下文切换到主线程? Or maybe only after 1 second there will be context switch to main thread? 或者,也许只有1秒之后,上下文才会切换到主线程?

i don't know how it works... I think there are two possibilities: 我不知道它是如何工作的...我认为有两种可能性:

1) System many times switches to main context, after that checks that time is not over and switches, does nothing and switches to another thread 2) There is system interruption that switches context when time is over 1)系统多次切换到主上下文,然后检查时间是否结束并切换,不执行任何操作并切换到另一个线程2)系统中断,时间结束时切换上下文

AFAIK it depends on the OS's scheduler implementation. AFAIK它取决于操作系统的调度程序实现。 But modern operating systems nowadays are clever enough to put the thread that has explicitly requested a temporary suspension in a different queue so that it won't be scheduled to take the CPU. 但是,当今的现代操作系统非常聪明,可以将明确要求临时挂起的线程放在另一个队列中,这样就不会计划将其占用CPU。
So, 所以,

Are there any context switching to main thread during this time? 在这段时间内是否有任何上下文切换到主线程?

No. 没有。

Or maybe only after 1 second there will be context switch to main thread? 或者,也许只有1秒之后,上下文才会切换到主线程?

Yes, after 1 second or more. 是的,在1秒或更长时间后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM