简体   繁体   English

关于pthread_cond_wait?

[英]about the pthread_cond_wait?

I have the following code: 我有以下代码:

typedef struct {
...
    volatile int i_lines_completed;
    pthread_mutex_t mutex;
    q265_pthread_cond_t cv;
...
}q265_picture_t;
void q265_frame_cond_broadcast( q265_picture_t *frame, int i_lines_completed )
{
    pthread_mutex_lock( &frame->mutex );
    frame->i_lines_completed = i_lines_completed;
    pthread_cond_broadcast( &frame->cv );
    pthread_mutex_unlock( &frame->mutex );
}
void q265_frame_cond_wait( q265_picture_t *frame, int i_lines_completed )
{
    pthread_mutex_lock( &frame->mutex );
    while( frame->i_lines_completed < i_lines_completed )
        pthread_cond_wait( &frame->cv, &frame->mutex );
    pthread_mutex_unlock( &frame->mutex );
}

The use case is: 用例是:

More than one thread could call q265_frame_cond_wait to request that the frame have the required i_lines_completed while there is only one thread call the q265_frame_cond_broadcast to broadcast the i_lines_completed . 多个线程可以调用q265_frame_cond_wait来请求帧具有所需的i_lines_completed而只有一个线程调用q265_frame_cond_broadcast来广播i_lines_completed

The question is: 问题是:

Is it valid that several threads call the q265_frame_cond_wait synchronously? 几个线程同步调用q265_frame_cond_wait是否有效?

When the certain thread call the q265_frame_cond_broadcast , 当某个线程调用q265_frame_cond_broadcast

  • Will all the waiting threads get the mutex synchronously? 所有等待的线程是否会同步获取互斥锁?
  • Or they must compete to get the mutex? 或者他们必须竞争获得互斥量?

Another problem: But is it right that two pthread_cond_t share only one mutex? 另一个问题:但两个pthread_cond_t只共享一个互斥锁是对的吗? For example, the following code, the two pthread_cond_t is_fill and is_empty share the only one mutex, and threads will possibly call q265_framelist_cond_wait0 and q265_framelist_cond_wait1 synchronously. 例如,以下代码,两个pthread_cond_t is_fill和is_empty共享唯一的一个互斥锁,并且线程可能同步调用q265_framelist_cond_wait0和q265_framelist_cond_wait1。

typedef struct {
...
    volatile int i_size;
    pthread_mutex_t mutex;
    q265_pthread_cond_t is_fill, is_empty;
...
}q265_picture_list_t;
void q265_framelist_cond_wait0( q265_picture_list_t *framelist)
{
    pthread_mutex_lock( &framelist->mutex );
    while( framelist->i_size <= 0)
        pthread_cond_wait( &framelist->is_fill, &framelist->mutex );
    pthread_mutex_unlock( &framelist->mutex );
}
void q265_framelist_cond_wait1( q265_picture_list_t *framelist)
{
    pthread_mutex_lock( &framelist->mutex );
    while( framelist->i_size == max_size)
        pthread_cond_wait( &framelist->is_empty, &framelist->mutex );
    pthread_mutex_unlock( &framelist->mutex );
}

The question is: Is it valid that several threads call the q265_frame_cond_wait synchronously 问题是:多个线程同步调用q265_frame_cond_wait是否有效

Multiple threads can call q265_frame_cond_wait , there is no race condition. 多个线程可以调用q265_frame_cond_wait ,没有竞争条件。

q265_frame_cond_broadcast , will all the waiting thread get the mutex synchronously? q265_frame_cond_broadcast ,所有等待的线程是否会同步获取互斥锁?

pthread_cond_broadcast wakes up all threads currently waiting on the condition variable. pthread_cond_broadcast唤醒当前正在等待条件变量的所有线程。 Only one thread can lock a mutex at a time, so that these woken up threads queue up on locking the mutex. 一次只有一个线程可以锁定互斥锁,因此这些唤醒的线程会在锁定互斥锁时排队。

Or they must compete to get the mutex? 或者他们必须竞争获得互斥量?

Conceptually yes, pthread_cond_wait has to lock the mutex on return. 从概念上讲,是的, pthread_cond_wait必须在返回时锁定互斥锁。 And this is known as thundering herd problem . 这被称为雷鸣般的群体问题

Linux solves this problem by moving the queue of waiters on the condition variable to the queue of waiters on the mutex to avoid waking up threads that would then immediately be blocked on the mutex. Linux通过将条件变量上的服务器队列移动到互斥锁上的服务器队列来解决此问题,以避免唤醒线程,然后立即在互斥锁上阻塞。 This is known as wait morphing . 这称为等待变形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM