c++ - pthread_cond_t 和 std::condition_variable 的区别

标签 c++ pthreads mutex condition-variable

我最近正在测试std::condition_variable,测试后发现它与pthread_cond_t有很大的不同,我想知道我的测试是否有错误?还是 std::condition_variable 与 pthread_cond_t 真的完全不同?

pthread_cond_t 源代码如下,在 gcc 4.4.6 编译:

pthread_cond_t  condA  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int ProcessRow = 0 ;
#define LOOPCNT 10

void *producer()
{
    int idx ;
    for(idx=0;idx<LOOPCNT;idx++)
    {
        //pthread_mutex_lock(&mutex);
        __sync_add_and_fetch(&ProcessRow,1) ;
        pthread_cond_signal(&condA);
        printf("sending signal...(%d)\n",ProcessRow) ;
        //pthread_mutex_unlock(&mutex);
    }
    printf("I am out ... \n") ;
}

void *consumer()
{
    int icnt = 0 ;
    while(1)
    {
        pthread_mutex_lock(&mutex);
        while (ProcessRow <= 0)
            pthread_cond_wait(&condA, &mutex);
        pthread_mutex_unlock(&mutex); // I forget to add unlock to fail this test
        __sync_sub_and_fetch(&ProcessRow,1) ;
        ++icnt ;
        printf("receving=(%d)\n",ProcessRow) ;
        usleep(10000) ;
    }
    printf("(%d)\n",ProcessRow) ;
}

输出:

sending signal...(1)
sending signal...(2)
sending signal...(3)
sending signal...(4)
sending signal...(5)
sending signal...(6)
sending signal...(7)
sending signal...(8)
sending signal...(9)
sending signal...(10)
I am out ...
receving=(9)

看起来像 pthread_cond_wait 中的 comsumer 线程 block ,因此“接收”仅打印 一次 !!!!

然后下面的测试是针对 std::condition_variable !!!!

下面的binsem.hpp来自 https://gist.github.com/yohhoy/2156481
稍作修改,编译为 g++ 4.8.1

class binsem {
public:    
    explicit binsem(int init_count = count_max)      
   : count_(init_count) {}     
// P-operation / acquire    
void wait()    
{        
    std::unique_lock<std::mutex> lk(m_);        
    cv_.wait(lk, [this]{ return 0 < count_; });
    --count_;    
}    
bool try_wait()    
{        
    std::lock_guard<std::mutex> lk(m_);        
    if (0 < count_) 
    {            
        --count_;            
        return true;        
    } else 
    {            
        return false;        
    }    
}
// V-operation / release    
void signal()    
{        
    std::lock_guard<std::mutex> lk(m_);

    //if (count_ < count_max)  // I mark here
    //{  // I mark here
            ++count_; 
            cv_.notify_one();        
    //}    // I mark here
}     
// Lockable requirements    
void lock() { wait(); }    
bool try_lock() { return try_wait(); }    
void unlock() { signal(); } 
private:    
    static const int count_max = 1;    
    int count_;    
    std::mutex m_;    
    std::condition_variable cv_;
}; 

和我的来源:

#define LOOPCNT 10
atomic<int>  ProcessRow  ;

void f4()
{
    for(int i=0;i<LOOPCNT;i++)
    {
        sem2.unlock() ;
        ++ProcessRow ;
    }
    cout << "i am out" << endl ;
}

void f5()
{
    int icnt = 0 ;
    std::chrono::milliseconds sleepDuration(1000);
    while(1)
    {
        sem2.lock() ;
        ++icnt ;
        std::this_thread::sleep_for(sleepDuration);
        cout << ProcessRow << "in f5 " << endl ;
        --ProcessRow ;
        if(icnt >= LOOPCNT)
            break ;
     }
     printf("(%d)\n",icnt) ;
}

输出:

i am out
10in f5
9in f5
8in f5
7in f5
6in f5
5in f5
4in f5
3in f5
2in f5
1in f5
(10)

看起来信号只有在 pthread_cond_wait 正在等待时才有效!!否则,信号丢失!!

对于 std::condition_variable ,看起来 std::condition_variable.wait() 会在调用 notify_one() 时唤醒,如果你在 10 秒前调用 notify_one() 然后调用 wait() ,std::condition_variable.wait() 仍然会收到 notify_one() 消息,与 pthread_cond_t 完全不同!!

我在这个测试中错过了什么吗?或者就像我的测试一样,std::condition 和 pthread_cond_t 就像测试显示的一样?

编辑:

我觉得下面这个测试会更容易一些,很抱歉忘记解锁导致测试失败,他们是一样的行为!!!!

int main()
{
    //pthread_mutex_lock(&mutex);
    ++ProcessRow ;
    pthread_cond_signal(&condA);
    //pthread_mutex_unlock(&mutex);
    printf("sending signal...\n") ;
    sleep(10) ;

    pthread_mutex_lock(&mutex);
    while (ProcessRow <= 0)
        pthread_cond_wait(&condA, &mutex);
    pthread_mutex_unlock(&mutex);
    printf("wait pass through\n") ;
}

这将显示:

sending signal...
wait pass through

对于 std::condition_variable

int main()
{
sem2.unlock() ;
std::chrono::milliseconds sleepDuration(10000);
cout << "going sleep" << endl ;
std::this_thread::sleep_for(sleepDuration);
sem2.lock() ;
cout << "lock pass through " << endl ;

} 

将显示:

going sleep
lock pass through

所以测试错误是我的错,导致死锁!感谢您提供的所有宝贵建议!

最佳答案

在您的 pthread 代码中,您永远不会解锁互斥量,consumer() 函数会在第二次迭代时死锁。此外,外层的 while 循环应该在满足某些条件时中断。我建议它应该在 icnt 到达 LOOPCNT 时爆发。这种类型与您在 f5() 中打破循环的方式相匹配。

void *consumer(void *x)
{
    int icnt = 0 ;
    while(1)
    {
        pthread_mutex_lock(&mutex);
        while (ProcessRow <= 0)
            pthread_cond_wait(&condA, &mutex);
        __sync_sub_and_fetch(&ProcessRow,1) ;
        ++icnt ;
        printf("receving=(%d) icnt=(%d)\n",ProcessRow, icnt) ;
        pthread_mutex_unlock(&mutex);
        if (icnt == LOOPCNT) break;
        usleep(10000) ;
    }
    printf("(%d)\n",ProcessRow) ;
}

您的代码的 std::thread 版本似乎与 pthread 版本完全不匹配,因此我认为您无法通过这种方式比较它们的执行情况。与其模仿信号量,不如像在代码的 pthread 版本中一样使用 std::condition_variable 更好。这样,您就可以真正将苹果与苹果进行比较。

std::condition_variable condA;
std::mutex mutex;
volatile int ProcessRow = 0 ;
#define LOOPCNT 10

void producer()
{
    int idx ;
    for(idx=0;idx<LOOPCNT;idx++)
    {
        std::unique_lock<std::mutex> lock(mutex);
        __sync_add_and_fetch(&ProcessRow,1) ;
        condA.notify_one();
        printf("sending signal...(%d)\n",ProcessRow) ;
    }
    printf("I am out ... \n") ;
}

void consumer()
{
    int icnt = 0 ;
    while(icnt < LOOPCNT)
    {
        if(icnt > 0) usleep(10000);
        std::unique_lock<std::mutex> lock(mutex);
        while (ProcessRow <= 0)
            condA.wait(lock);
        __sync_sub_and_fetch(&ProcessRow,1) ;
        ++icnt ;
        printf("receving=(%d) icnt=(%d)\n",ProcessRow, icnt) ;
    }
    printf("(%d)\n",ProcessRow) ;
}

关于c++ - pthread_cond_t 和 std::condition_variable 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222137/

相关文章:

C++调用带可变参数的lua函数

linux-kernel - 防止linux内核将线程迁移到其他cpu的任何方法

c - 段错误(核心转储)pthread

python - QMutexLocker() 导致 UI 卡住

multithreading - rust "future cannot be sent between threads safely"

c++ - 更好的 vector 数据压缩算法?

c++ - 降雨统计 C++

delphi - Delphi 中的 TMutex 是可重入的吗?

c++ - 尝试访问 vector 时出现段错误 - cpp

c - 如何获取 pthread_attr_t 上的 detachstate?