简体   繁体   English

pthread_mutex_unlock如何区分线程?

[英]How pthread_mutex_unlock distinguish threads?

Only the owner of mutex can unlock it. 只有互斥锁的所有者才能解锁它。 But how mutex distinguish thread that locked it? 但是互斥锁如何区分锁定它的线程? Does threads have any distinctive features in Linux? 线程在Linux中是否具有任何独特的功能?

You can look at the implementation source code for details (the pthread implementation from the GNU libc Git repository can be browsed here ), but they have different IDs that are used internally. 您可以查看实现源代码以获取详细信息(可以在此处浏览GNU libc Git存储库的pthread实现),但是它们具有内部使用的不同ID。 You can see this at the application level using pthread_self() . 您可以使用pthread_self()在应用程序级别看到它。 It returns a pthread_t value that is unique on a per-thread basis within a given process. 它返回一个pthread_t值,该值在给定进程中基于每个线程是唯一的。 There is no guarantee of uniqueness when you compare pthread_t values from different processes. 比较不同进程的pthread_t值时,不能保证唯一性。

The actual type that pthread_t corresponds to is implementation-defined, however; 但是, pthread_t对应的实际类型是实现定义的; it could be an arithmetic (eg integral) type, or it could be a structure. 它可以是算术(例如整数)类型,也可以是结构。 Therefore, you can't really do much with them in a portable way, other than compare them for equality using pthread_equal() . 因此,除了使用pthread_equal()比较它们的相等性以外,您实际上不能以可移植的方式对其进行过多处理。

它们使用线程ID进行区分;

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

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