简体   繁体   中英

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.

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?

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

AFAIK it depends on the OS's scheduler implementation. 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.
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?

Yes, after 1 second or more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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