本文介绍了理解 pthread_cond_wait() 和 pthread_cond_signal()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,pthread_cond_wait()pthread_cond_signal()的调用如下:

Generally speaking, pthread_cond_wait() and pthread_cond_signal() are called as below:

//thread 1:
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
do_something()
pthread_mutex_unlock(&mutex);

//thread 2:
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

步骤是

  1. pthread_cond_wait(&cond, &mutex); 被调用,它解锁互斥锁

  1. pthread_cond_wait(&cond, &mutex); is called, it unlocks the mutex

线程 2 锁定互斥锁并调用 pthread_cond_signal(),从而解锁互斥锁

Thread 2 locks the mutex and calls pthread_cond_signal(), which unlocks the mutex

在线程 1 中,pthread_cond_wait() 被调用并再次锁定互斥锁

In thread 1, pthread_cond_wait() is called and locks the mutex again

现在在线程 2 中,在调用 pthread_cond_signal() 之后,pthread_mutex_unlock(&mutex) 将要运行,在我看来它要解锁一个现在被线程 1 锁定的互斥锁.我的理解有什么问题吗?

Now in thread 2, after pthread_cond_signal() is called, pthread_mutex_unlock(&mutex) is going to run, it seems to me that it wants to unlock a the mutex which is now locked by thread 1. Is there anything wrong in my understanding?

此外,在我看来,pthread_cond_wait() 只能由 1 个线程为同一个 cond-mutex 对调用.但是有一种说法pthread_cond_signal() 函数应至少解除阻塞在指定条件变量 cond 上的线程之一(如果有任何线程在 cond 上被阻塞)."那么,这是否意味着 pthread_cond_wait() 可以被多个线程为同一个 cond-mutex 对调用?

Besides, it also seems to me that pthread_cond_wait() can be called by only 1 thread for the same cond-mutex pair. But there is a saying "The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond)." So, it means pthread_cond_wait() can be called by many threads for the same cond-mutex pair?

推荐答案

pthread_cond_signal 不解锁互斥锁(它不能,因为它没有对互斥锁的引用,所以它怎么知道是什么?解锁?)实际上,信号不需要与互斥锁有任何联系;信号线程不需要持有互斥锁,但对于大多数基于条件变量的算法来说,它会.

pthread_cond_signal does not unlock the mutex (it can't as it has no reference to the mutex, so how could it know what to unlock?) In fact, the signal need not have any connection to the mutex; the signalling thread does not need to hold the mutex, though for most algorithms based on condition variables it will.

pthread_cond_wait 在它睡觉之前解锁互斥锁(如您所见),但是当它被发出信号时,它会在它醒来之前重新获取互斥锁(这可能需要等待).所以如果信号线程持有互斥锁(通常情况下),等待线程将不会继续,直到信号线程也解锁互斥锁.

pthread_cond_wait unlocks the mutex just before it sleeps (as you note), but then it reaquires the mutex (which may require waiting) when it is signalled, before it wakes up. So if the signalling thread holds the mutex (the usual case), the waiting thread will not proceed until the signalling thread also unlocks the mutex.

条件变量的常见用法如下:

The common use of condition vars is something like:

thread 1:
    pthread_mutex_lock(&mutex);
    while (!condition)
        pthread_cond_wait(&cond, &mutex);
    /* do something that requires holding the mutex and condition is true */
    pthread_mutex_unlock(&mutex);

thread2:
    pthread_mutex_lock(&mutex);
    /* do something that might make condition true */
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);

这两个线程有​​一些共享数据结构,互斥锁保护访问.第一个线程想要等到某个条件为真,然后立即执行一些操作(没有其他线程在条件检查和操作之间进入并使条件为假的竞争条件机会.)第二个线程正在做一些可能使条件为真,因此它需要唤醒可能正在等待它的任何人.

The two threads have some shared data structure that the mutex is protecting access to. The first thread wants to wait until some condition is true then immediately do some operation (with no race condition opportunity for some other thread to come in between the condition check and action and make the condition false.) The second thread is doing something that might make the condition true, so it needs to wake up anyone that might be waiting for it.

这篇关于理解 pthread_cond_wait() 和 pthread_cond_signal()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:40
查看更多