I have been trying to use some ways to prevent Priority Inversion problem but I still have deadlock issue.
I found out that there are some protocols which <pthread.h>
library provide such that PTHREAD_PRIO_INHERIT
and PTHREAD_PRIO_PROTECT
. I set this protocols to my mutexes but it seems doesn't work properly.
My example is:
Three task is exist at the same time -> T1 priority(20), T2 priority(20), T3 priority(20)
And there are two mutexes which are using by all threads in two different functions.
So my question is that can we avoid this problem by using the only these protocol attributes to our mutexes or do I need further job after setting this protocols?
pthread_mutexattr_t mta;
pthread_mutexattr_init(&mta);
pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT);
int ret = pthread_mutex_init(this->native_handle(), &mta);
pthread_mutexattr_destroy(&mta);
there above a code snippet where I try to set PTHREAD_PRIO_INHERIT
protocol to my c++ std mutex.
static void set_scheduling_params(std::thread &th, int policy, int priority) {
sched_param sch_params;
sch_params.sched_priority = priority;
if(pthread_setschedparam(th.native_handle(), policy, &sch_params)) {
std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
}
}
Above a function where I set my scheduling and priority. I set the scheduling policy as SCHED_FIFO.