Reputation: 1397
I'm experiencing an issue where a thread is somehow being preempted by a lower-priority thread, despite the fact that the higher-priority thread doesn't make any blocking calls. I've noticed that when the lower-priority thread does preempt the higher-priority thread, the higher-priority thread is in the middle of a system call. Specifically, ReleaseMutex and Sleep(0). Is it possible that the system call is doing something that would block the current thread and allow a lower priority thread to run?
Upvotes: 1
Views: 104
Reputation: 182827
Priority just means that there is some amount of preference for one thread over another. It can always be the case that a thread can't make forward progress for some reason and a lower-priority thread then pre-empts it.
Imagine, for example, if ReleaseMutex
happens to wind up in some rare code path due to some strange edge case and the code for handling that edge case has paged out to disk. The thread that called ReleaseMutex
is not ready-to-run until that code pages in, so a lower-priority thread can get the CPU.
I don't think that's a particularly likely scenario. But the point is that it's not guaranteed not to happen. Priority is not an exclusionary method or a synchronization mechanism. It's just a way of indicating what you prefer when the system happens to have a choice.
If this is causing you an issue, you have something very wrong in your design. If you're trying to use thread priorities as a way of guaranteeing particular behavior (rather than indicating preferences) you are handling exclusion entirely wrong.
Upvotes: 3