1
votes

If a pthread owns a mutex and then calls pthread_create, does the new thread own the mutex, do both, or does only the original thread?

1

1 Answers

3
votes

The original thread still owns the mutex. Creating threads has no effect on mutex ownership.

Were it any other way, it would be extremely difficult to call code that might create a thread because that code would have to know about every mutex it held.

For example, suppose you implemented some collection class that had a sort algorithm. You have no idea what mutexes callers might be holding when they call your sort function. What do you do if you want to change the sort algorithm to use a few helper threads? What do you do about the mutexes you might or might not hold? It would create an impossible situation.