我正在写一个简单的生产者/消费者程序,以更好地理解C++和多线程。 在我的线程运行过的消费者,我有这些前两行:C++简单线程问题
pthread_cond_wait(&storageCond, &storageMutex);
pthread_mutex_lock(&storageMutex);
但程序卡住了,可能是一个僵局。 然后我换行:
pthread_mutex_lock(&storageMutex);
pthread_cond_wait(&storageCond, &storageMutex);
和它的工作。 有人可以帮我理解为什么这个工作,前者没有?
谢谢。