views:

77

answers:

1

Consider following situation, assuming single CPU system:

  • thread A is running with a priority THREAD_PRIORITY_NORMAL, signals event E
  • thread B with a priority THREAD_PRIORITY_LOWEST is waiting for an event E (Note: at this point the thread is not scheduled because it is runnable, but A is higher priority and runnable as well)
  • thread A calls SetThreadPriority(B, THREAD_PRIORITY_ABOVE_NORMAL)

Is thread B re-scheduled immediately to run, or is thread A allowed to continue until current time-slice is over, and B is scheduled only once a new time-slice has begun?

I would be interested to know the answer for WinXP, Vista and Win7, if possible.

Note: the scenario above is simplified from my real world code, where multiple threads are running on multiple cores, but the main object of the question stays: does SetThreadPriority cause thread scheduling to happen?

Edit: If the scheduling does not happen during SetThreadPriority, are there some common patterns how to make it happen?

+1  A: 

We've been in this situation before - no, resheduling will only happen when a thread performs some wait operation. The easiest is to call Sleep().

For example, see this paper - authors need their code to be switched between processor cores and they manually call Sleep() for that.

sharptooth
Nice. I was under impression Sleep(0) gives up the rest of the slice even if there are no runnable threads, but reading docs it seems I was wrong. (The behaviour I was expecting from Sleep(0) can be most closely achieved by using Sleep(1).)
Suma