我正在寫一個簡單的生產者/消費者程序,以更好地理解C++和多線程。 在我的線程運行過的消費者,我有這些前兩行:C++簡單線程問題
pthread_cond_wait(&storageCond, &storageMutex);
pthread_mutex_lock(&storageMutex);
但程序卡住了,可能是一個僵局。 然後我換行:
pthread_mutex_lock(&storageMutex);
pthread_cond_wait(&storageCond, &storageMutex);
和它的工作。 有人可以幫我理解爲什麼這個工作,前者沒有?
謝謝。