Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between PTHREAD_PRIO_INHERIT and PTHREAD_PRIO_PROTECT?

In the following function:

int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr,
   int protocol);

You can define the protocol for a mutex attribute. Following protocols are available:

  • PTHREAD_PRIO_NONE
  • PTHREAD_PRIO_INHERIT
  • PTHREAD_PRIO_PROTECT

I know when PTHREAD_PRIO_INHERIT is selected, the task which holds the lock get the highest priority of all tasks(process(if shared between processes) or thread) which you are blocking. This prevents against priority inversion. But PTHREAD_PRIO_PROTECT seems to do the same. So my question is, what is the difference between both protocols ?

like image 840
cominfotty Avatar asked Dec 23 '17 14:12

cominfotty


1 Answers

See the specification in POSIX.

In short, the differences ares:

  • With PTHREAD_PRIO_INHERIT, the thread holding the lock inherits the priority of the highest-priority thread currently contending for the lock (no elevated priority if no other threads are blocked trying to get the lock).

  • With PTHREAD_PRIO_PROTECT, the thread holding the lock always runs with elevated priority determined not by other threads contending for the lock, but by a property of the lock (its priority ceiling) configured on a per-lock basis.

You may also want to see the Wikipedia article on the topic to understand the motivations by which one or the other may be chosen.

like image 170
R.. GitHub STOP HELPING ICE Avatar answered Oct 16 '22 01:10

R.. GitHub STOP HELPING ICE