Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the modern C++ library support threads with priority?

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?

like image 313
vard Avatar asked Dec 09 '22 07:12

vard


1 Answers

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.

like image 151
Jerry Coffin Avatar answered Mar 05 '23 02:03

Jerry Coffin