当线程在已解锁的互斥体上调用 pthread_mutex_unlock 时会发生什么

What happens when a thread calls pthread_mutex_unlock on an already unlocked 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)。所以这意味着互斥量已解锁,现在如果 parent_thread 尝试调用 pthread_mutex_unlock(&mutex),它应该会导致未定义的行为,对吗?

示例代码:

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;
}

So this would mean that the mutex is unlocked

否,pthread_cond_wait returns 已锁定互斥锁。

if parent_thread attempts to call pthread_mutex_unlock(&mutex)

Parent 只是在 pthread_cond_wait returns.

之后解锁互斥量

抱歉,我评论的时候才看你的标题。只是为了确保我理解您的操作顺序,already-posted 答案的 long-winded 版本:

parent 调用:

pthread_mutex_lock(&m); // locks the mutex
pthread_cond_wait(&c, &m);  // releases the mutex

child 调用:

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 调用:

// 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

当然,这只是几个操作顺序中的一个。我认为你的误解是 pthread_cond_wait 在获得互斥量之前不能 return 成功。那时,它在锁定的互斥体上正确调用 pthread_mutex_unlock(&m);