我想用C++來解決Dining philosophers problem。pthread_cond_wait奇怪的行爲
代碼編譯爲g++ -lpthread
。
整個解決方案在philosophers github。存儲庫包含兩個cpp文件:main.cpp和philosopher.cpp。 「Main.cpp」創建互斥變量,信號量,5個條件變量,5個分支,並啓動哲學家。信號量僅用於同步哲學家的開端。其他參數傳遞給哲學家來解決問題。 「Philosopher.cpp」包含給定問題的解決方案,但發生幾個步驟後會發生死鎖。
當哲學家0吃東西時,會發生僵局,哲學家1(在他旁邊)想要分叉。然後,哲學家1已經採取互斥,並且不會放棄,直到哲學家0放下他的叉子。由於被互斥了,哲學家0不能放下他的叉子,所以我們陷入了僵局。問題出在Philosopher :: take_fork方法中,調用pthread_cond_wait(a,b)不釋放互斥量b。無法弄清楚爲什麼?
// Taking fork. If eather lef or right fork is taken, wait.
void Philosopher::take_fork(){
pthread_mutex_lock(&mon);
std::cout << "Philosopher " << id << " is waiting on forks" << std::endl;
while(!fork[id] || !fork[(id + 1)%N])
pthread_cond_wait(cond + id, &mon);
fork[id] = fork[(id + 1)%N] = false;
std::cout << "Philosopher " << id << " is eating" << std::endl;
pthread_mutex_unlock(&mon);
}
其餘的請參考this code。
謝謝。問題在於未初始化所有條件變量。 – akegalj 2013-03-02 16:16:46