How to prevent threads from starvation in C ++ 11

I'm just wondering if there is any blocking policy in C ++ 11 that would prevent hunger from threads.

I have a bunch of threads that compete for one mutex. Now my problem is that the thread leaving the critical section immediately begins to compete for the same mutex and wins most of the time. Therefore, other threads awaiting the mutex are starving.

I donโ€™t want the thread to leave a critical section and sleep for a minimum amount of time to give other threads the ability to lock the mutex.

I thought that there should be some parameter that would provide a fair lock for threads waiting for the mutex, but I could not find a suitable solution.

Well, I found the function std :: this_thread :: yield (), which suggests rescheduling the thread execution order, but this is only a hint at the scheduler thread and depends on the implementation of the scheduler thread if it redirects threads or not.

Is there a way to provide a fair blocking policy for threads expecting the same mutex in C ++ 11? What are the usual strategies?

thanks

+6
source share
1 answer

This is a common optimization in mutexes, designed to avoid wasting time when switching tasks, when the same thread can accept the mutex again. If the current thread still has time remaining in the time frame, you get more bandwidth in terms of user instructions executed per second, allowing it to accept the mutex rather than pause it, and switch to another thread (which probably causes a large reloading cache lines and various other delays).

If you have so much disputes over the mutex that this is a problem, then your application design is wrong. You have all these threads blocked on the mutex, and therefore do nothing: you probably better not need so many threads.

You must design your application so that if multiple threads compete for a mutex, it does not matter which thread gets the lock. Direct rivalry should also be a rare thing, especially direct rivalry with a lot of flows.

The only situation where I can think that this is an โ€œOKโ€ scenario is where each thread expects a condition variable, which is then translated to wake them all up. Each thread will fight for the mutex, but if you do it right, everyone should quickly check that this is not a side track, and then release the mutex. Even then, this is called a โ€œthunder-herdโ€ situation and is not ideal, precisely because it serializes all these streams.

+6
source

All Articles