1

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.

methamphetamine
  • 148
  • 1
  • 5
  • Add some code to where you facing issue. Check https://stackoverflow.com/help/minimal-reproducible-example – Manthan Tilva Dec 13 '19 at 07:03
  • Its not even clear what your question is.... my guess is that you have a basic mutex lockout issue, but you are misdiagnosing it as a priority problem. Unless the problem only occurs on a 2 thread CPU and not a 3 thread one, how could it be a priority issue? – Secto Kia Dec 13 '19 at 09:49
  • The problem can be solved by priority ceiling protocol but I am trying to find out whether is it possible to apply with linux system calls or not – methamphetamine Dec 13 '19 at 11:11

0 Answers0