user275402
user275402

Reputation: 195

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?

Upvotes: 0

Views: 82

Answers (2)

sramij
sramij

Reputation: 4925

They are differentiated using the thread id;

Upvotes: 2

Jason R
Jason R

Reputation: 11758

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. You can see this at the application level using pthread_self(). It returns a pthread_t value that is unique on a per-thread basis within a given process. There is no guarantee of uniqueness when you compare pthread_t values from different processes.

The actual type that pthread_t corresponds to is implementation-defined, however; it could be an arithmetic (e.g. 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().

Upvotes: 3

Related Questions