沉静
  
  首页 >>  
我的日历
分类日志
友情链接
最新评论
搜索日志
访问计数
获取 RSS
我的 Blog:
youalwayscan 最新的 20 条日志
[心情留言]
[点滴积累]
[好文共赏]
[C/C+基础]
[Unix/Linux基础]
[WxWidgets]
[VC/MFC]
全站 Blog:
全站最新的 20 条日志

 

生产者/消费者问题(信号量、VC++实现)

   C/C+基础2005-3-29 14:50

利用信号量和VC++程序设计环境实现生产者/消费者问题。


/////////////////////////////////////////////////////////////////////


//! 定义信号量

HANDLE m_S_Producer; // Semaphore
HANDLE m_S_Consumer; // Semaphore
HANDLE m_E_Queue; // Event


//! 假设仓库最多容纳M个物品,开始仓库为空

m_S_Producer = CreateSemaphore(NULL, M, M, NULL); //初始计数为M
m_S_Consumer = CreateSemaphore(NULL, 0, M, NULL); //初始计数为0
m_E_Queue = CreateEvent(NULL, FALSE, TRUE, NULL); //自动类型,初始状态为信号态


//!生产者

if (WaitForSingleObject(m_S_Producer, WaitTime) == WAIT_OBJECT_0)
{
if (WaitForSingleObject(m_E_Queue, WaitTime) == WAIT_OBJECT_0)
{// OK now, put product

// relase comsumer’s semaphore
ReleaseSemaphore(m_S_Consumer, 1, NULL);
// set event to signal
SetEvent(m_E_Queue);
return TRUE;
}

else // wait event time out

{
// not put product so release producer
ReleaseSemaphore(m_S_Producer, 1, NULL);
SetEvent(m_E_Queue);
return FALSE;
}
}

else // wait semaphore time out
{
return FALSE;
}
}



//!消费者

if (WaitForSingleObject(m_S_Consumer, WaitTime) == WAIT_OBJECT_0)

{
if (WaitForSingleObject(m_E_Queue, WaitTime) == WAIT_OBJECT_0)
{// OK now, Get product then
// releasee productor’s semaphore
ReleaseSemaphore(m_S_Producer, 1, NULL);
// set event to signal
SetEvent(m_E_Queue);
return TRUE;
}

else // wait event time out
{// not get product so release Consumer

ReleaseSemaphore(m_S_Consumer, 1, NULL);
SetEvent(m_E_Queue);
return FALSE;
}
}

else // wait semaphore time out
{
return FALSE;
}

}



特别需要注意的是,在该模型中,对互斥量(本例为m_E_Queue)的等待必须放在第二个等待位置,否则可能将造成线程死锁。考虑这样的情况:仓库已满,没有任何线程访问。某一时刻,生产者放入物品,首先等待m_E_Queue并通过,且将m_E_Queue置于无信号态,然后等待m_S_Produce时被阻塞。这时某一消费者欲取物品,无论在什么情况下,都将在等待m_E_Queue时被阻塞,从而相互等待造成死锁。在释放同步对象时则位置不受限制。

标签集:TAGS:
回复Comments()点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}