How to allow certain threads to have priority in locking a mutex use PTHREADS

前端 未结 6 1238
天命终不由人
天命终不由人 2020-12-15 23:32

Assume that the following code is being executed by 10 threads.

pthread_mutex_lock(&lock)
Some trivial code
pthread_mutex_unlock(&lock)
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 00:02

    The native way is to enable priority inheritance for your mutex (with pthread_mutex_attr), and use pthread's thread priority to perform what you need. It only requires very few lines of code, and you are not re-inventing the wheel. On the good side, it'll also work with RT or FIFO scheduler while your homebrew version will not.

    Then, whenever a thread with a high priority waits on a mutex that's acquired by a thread on lower priority, the kernel "boost" the low priority thread so it can be scheduled in place of the high priority thread, thus giving it a timeslice to release the lock. As soon as the lock is released, the high priority thread is scheduled. That's the lowest delay you could get since it's done in the kernel.

提交回复
热议问题