Many third party C/C++ libraries providing multithreading support threads' priority, corresponding scheduler, etc. Why doesn't the modern C++ standard support this useful feature?
The short answer, I think, is that if the standard included a way to specify priorities, it would also have to specify what would happen as a result. This, unfortunately would lead to one of two possibilities: either you'd force people to completely re-implement threads from the ground up on systems that had different semantics, or else you'd limit the platforms to which code that used std::thread
could be ported.
Just for example, on some systems, threads of sufficiently high priority (e.g., "real time priority") use round-robin scheduling. Other systems don't--when a thread of sufficiently high priority starts, it will continue to be scheduled until it runs to completion or is interrupted by a thread of even higher priority. Specifying either behavior would lead to problems porting to systems that use the other.
Many (most?) systems also include some mechanism to prevent starving lower priority threads, so they can continue to receive some CPU time even while higher priority threads are ready to run. Again, the details vary and a few (especially smaller/simpler) systems simply don't include any such mechanism at all. As above, attempting to specify any one behavior would lead to difficulty in porting to systems that implement different behavior.
It would be easy to include a set_priority(int)
(or something similar), but specifying what it meant/did portably would be next to impossible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With