[英]What happens when a thread calls pthread_mutex_unlock on an already unlocked mutex
I know this question sounds familiar and it might even be a stupid question but I wasn't able to find the solution to it.我知道这个问题听起来很熟悉,甚至可能是一个愚蠢的问题,但我无法找到解决方案。
So my question basically is, what happens when a parent_thread
calls pthread_mutex_lock(&mutex)
then calls pthread_cond_wait(&condition, &mutex)
releasing the mutex, and then child_thread
calls pthread_mutex_lock(&mutex)
followed by pthread_cond_signal(&condition)
and then pthread_mutex_unlock(&mutex)
.所以我的问题基本上是,当
parent_thread
调用pthread_mutex_lock(&mutex)
然后调用pthread_cond_wait(&condition, &mutex)
释放互斥锁,然后child_thread
调用pthread_mutex_lock(&mutex)
后跟pthread_cond_signal(&condition)
然后pthread_mutex_unlock(&mutex)
会发生什么。 So this would mean that the mutex is unlocked and now if parent_thread
attempts to call pthread_mutex_unlock(&mutex)
, it should result in undefined behavior, right ?所以这意味着互斥锁已解锁,现在如果
parent_thread
尝试调用pthread_mutex_unlock(&mutex)
,它应该导致未定义的行为,对吧?
Example code:示例代码:
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
void thr_exit() {
pthread_mutex_lock(&m);
done = 1;
pthread_cond_signal(&c);
pthread_mutex_unlock(&m);
}
void *child(void *arg) {
printf("child\n");
thr_exit();
return NULL;
}
void thr_join() {
pthread_mutex_lock(&m);
while (done == 0)
pthread_cond_wait(&c, &m);
pthread_mutex_unlock(&m);
}
int main(int argc, char *argv[]) {
printf("parent: begin\n");
pthread_t p;
pthread_create(&p, NULL, child, NULL);
thr_join();
printf("parent: end\n"); return 0;
}
Sorry, only read your title when I commented.对不起,我评论的时候才看你的标题。 Just to make sure I understand your order of operations, a long-winded version of the already-posted answer:
只是为了确保我了解您的操作顺序,已发布答案的冗长版本:
parent calls:家长来电:
pthread_mutex_lock(&m); // locks the mutex
pthread_cond_wait(&c, &m); // releases the mutex
child calls:孩子打电话:
pthread_mutex_lock(&m); // locks the mutex
pthread_cond_signal(&c); // does nothing to the mutex, it's still locked
// the parent thread has been signaled, but it is still blocked
// because it can't acquire the mutex.
// At this moment in time, the child still owns the mutex, so
// pthread_cond_wait cannot acquire it, thus the parent waits...
pthread_mutex_unlock(&m); // releases the mutex
// ok, the child released the mutex and the parent thread has been signaled.
// The mutex is available. pthread_cond_wait in the parent can
// acquire it and return.
parent calls:家长来电:
// now that the mutex is unlocked, the parent can return from its
// pthread_cond_wait(&c, &m) call from above, which returns with the mutex
// locked. A successful call to pthread_cond_wait(..) returns with the
// mutex locked, so this can't successfully return until it acquires the
// mutex.
pthread_mutex_unlock(&m); // releases the locked mutex, no UB
Of course, this is only one of several order of operations.当然,这只是几个操作顺序之一。 I think your misunderstanding is that
pthread_cond_wait
can't return successfully until it acquires the mutex.我认为您的误解是
pthread_cond_wait
在获取互斥锁之前无法成功返回。 At that point, it correctly calls pthread_mutex_unlock(&m);
此时,它正确调用
pthread_mutex_unlock(&m);
on a locked mutex.在锁定的互斥体上。
So this would mean that the mutex is unlocked
所以这意味着互斥锁已解锁
No, pthread_cond_wait
returns with mutex locked.不,
pthread_cond_wait
返回并锁定互斥锁。
if parent_thread attempts to call pthread_mutex_unlock(&mutex)
如果 parent_thread 尝试调用 pthread_mutex_unlock(&mutex)
Parent just unlocks the mutex after pthread_cond_wait
returns.在
pthread_cond_wait
返回后,父级仅解锁互斥锁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.