Ensure that each thread gets a chance to execute in a given time period using C++11 threads

846 Views Asked by At

Suppose I have a multi-threaded program in C++11, in which each thread controls the behavior of something displayed to the user.

I want to ensure that for every time period T during which one of the threads of the given program have run, each thread gets a chance to execute for at least time t, so that the display looks as if all threads are executing simultaneously. The idea is to have a mechanism for round robin scheduling with time sharing based on some information stored in the thread, forcing a thread to wait after its time slice is over, instead of relying on the operating system scheduler.

Preferably, I would also like to ensure that each thread is scheduled in real time.

In case there is no way other than relying on the operating system, is there any solution for Linux?

Is it possible to do this? How?

2

There are 2 best solutions below

2
On BEST ANSWER

I want to ensure that for every time period T during which one of the threads of the given program have run, each thread gets a chance to execute for at least time t, so that the display looks as if all threads are executing simultaneously.

You are using threads to make it seem as though different tasks are executing simultaneously. That is not recommended for the reasons stated in Arthur's answer, to which I really can't add anything.

If instead of having long living threads each doing its own task you can have a single queue of tasks that can be executed without mutual exclusion - you can have a queue of tasks and a thread pool dequeuing and executing tasks.

If you cannot, you might want to look into wait free data structures and algorithms. In a wait free algorithm/data structure, every thread is guaranteed to complete its work in a finite (and even specified) number of steps. I can recommend the book The Art of Multiprocessor Programming where this topic is discussed in length. The gist of it is: every lock free algorithm/data structure can be modified to be wait free by adding communication between threads over which a thread that's about to do work makes sure that no other thread is starved/stalled. Basically, prefer fairness over total throughput of all threads. In my experience this is usually not a good compromise.

5
On

No that's not cross-platform possible with C++11 threads. How often and how long a thread is called isn't up to the application. It's up to the operating system you're using.


However, there are still functions with which you can flag the os that a special thread/process is really important and so you can influence this time fuzzy for your purposes.

You can acquire the platform dependent thread handle to use OS functions.

native_handle_type    std::thread::native_handle //(since C++11)

Returns the implementation defined underlying thread handle.

I just want to claim again, this requires a implementation which is different for each platform!


Microsoft Windows

According to the Microsoft documentation:

SetThreadPriority function

Sets the priority value for the specified thread. This value, together with the priority class of the thread's process determines the thread's base priority level.


Linux/Unix

For Linux things are more difficult because there are different systems how threads can be scheduled. Under Microsoft Windows it's using a priority system but on Linux this doesn't seem to be the default scheduling.

For more information, please take a look on this stackoverflow question(Should be the same for std::thread because of this).