简体   繁体   English

Mutex锁定主函数和pthread中其他线程之间的优先级

[英]Mutex lock priority between main function and the other threads in pthread

We can set thread priority by pthread_attr_t , which is the second parameter in pthread_create , but we cannot set the thread priority to the main function by pthread_attr_t . 我们可以通过设置线程的优先级pthread_attr_t ,这是第二个参数pthread_create ,但我们不能被线程优先级设置为主要功能pthread_attr_t

If the main function and the other thread both wait for the mutex lock, which will get the access to the mutex lock first ? 如果主要功能及其他线程都等待互斥锁,这将获得访问互斥锁第一

You cannot predict which thread among many will get a mutex first, regardless of their various priorities. 您无法预测许多线程中的哪个线程将首先获得互斥锁,而不管它们的各种优先级。

A thread with a higher priority will be run more frequently, and is more likely to get the lock, there is absolutely no guarantee. 具有更高优先级的线程将更频繁地运行,并且更有可能获得锁定,绝对没有保证。

Furthermore, if you ever design code that depends on threads getting locks in a particular order, then you are definitely designing the program incorrectly. 此外,如果您设计的代码依赖于线程以特定顺序获取锁定,那么您肯定是在错误地设计程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM