guojinsheng:算法中的这一段代码(在Eating函数中)有待商榷:
while(TRUE)
{
if(WaitForSingleObject(m_ph->tropics[(ID + THINKER_NUM -1)%THINKER_NUM],60+GetRandTime()) == WAIT_OBJECT_0)
{
if(WaitForSingleObject(m_ph->tropics[ID],100) != WAIT_OBJECT_0)
ReleaseMutex(m_ph->tropics[(ID + THINKER_NUM -1)%THINKER_NUM]);
else
break;
}
Sleep(600 + 10*(m_ph->m_speed)+GetRandTime());
}
考虑这样一种情况:当五个线程同时运行,而且完全同步运行,那么当执行到这一句时:
if(WaitForSingleObject(m_ph->tropics[(ID + THINKER_NUM -1)%THINKER_NUM],60+GetRandTime()) == WAIT_OBJECT_0)
每个线程的条件都会满足.然后又完全同步执行到下一句:
if(WaitForSingleObject(m_ph->tropics[ID],100) != WAIT_OBJECT_0)
每个线程的条件都不会满足.然后同时释放: ReleaseMutex(m_ph->tropics[(ID + THINKER_NUM -1)%THINKER_NUM]);
然后同时返回while处再做循环,如此往复.每个线程均无法走出while循环,发生死锁.
当然这种情况发生的概率极低.但的确存在这样一种极端的情况.所以次算法还有改进的地方.
(发表于2004-12-24 15:20:00)
..........................................................................
--------------------next---------------------
阅读(243) | 评论(0) | 转发(0) |