【发布时间】:2011-09-22 20:15:12
【问题描述】:
我正在编写一个简单的生产者/消费者程序来更好地理解 C++ 和多线程。 在运行消费者的线程中,我有以下前两行:
pthread_cond_wait(&storageCond, &storageMutex);
pthread_mutex_lock(&storageMutex);
但是程序卡住了,可能是死锁了。 然后我换线了:
pthread_mutex_lock(&storageMutex);
pthread_cond_wait(&storageCond, &storageMutex);
它奏效了。 有人可以帮我理解为什么这有效而前者无效吗?
谢谢。
【问题讨论】:
标签: c++ multithreading pthreads producer-consumer