mutex_lock return 在 C 中的顺序

Order of mutex_lock return in C

假设有 3 个线程 A、B 和 C。 还有一个互斥体 X

A 在 X 上调用 pthread_mutex_lock。

B 在 X 上调用 pthread_mutex_lock。

*

C 在 X 上调用 pthread_mutex_lock。

A 在 X 上调用 pthread_mutex_unlock。

**

现在谁可以首先使用共享资源? B还是C? 如果我尝试在 * 中使用 pthread_destroy_mutex 会怎样?在 **?

Who will now be able to use the shared resource first?

这取决于内核调度程序的实现。 Posix 这样说:

If there are threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock() is called, resulting in the mutex becoming available, the scheduling policy shall determine which thread shall acquire the mutex.

And what happens if I try to use pthread_destroy_mutex in *? in **?

再次来自 manual:

Attempting to destroy a locked mutex results in undefined behavior.

如果您对这些方面还有任何疑问,建议您查阅相关的手册页。