1

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

Igor R.
  • 14,716
  • 2
  • 49
  • 83
peter55555
  • 1,413
  • 1
  • 19
  • 36
  • if your thread is sleeping there is no reason for your OS to scheduele a time slice for that thread, so no. – David Haim Oct 11 '16 at 13:16
  • This is most likely implementation dependent, see also [this question](http://stackoverflow.com/questions/17325888/c11-thread-waiting-behaviour-stdthis-threadyield-vs-stdthis-thread). – Igor R. Oct 11 '16 at 13:19

1 Answers1

0

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.

Ahmad Siavashi
  • 979
  • 1
  • 12
  • 29