正在阅读:VC++学习:用vc实现生产者消费者问题VC++学习:用vc实现生产者消费者问题

2004-02-14 09:34 出处:CSDN 作者:windcao 责任编辑:linjixiong

  pBuf=this->m_Data;
  bool bSleep;
  for(int i=0;i {
  pDlg->m_pSemaphoreFull->Lock();
  pDlg->m_pMutex->Lock();
  sprintf(pBuf,pDlg->m_sBuf);
  bSleep=pDlg->m_bSlow ;
  pDlg->m_pMutex->Unlock();
  pBuf+=10;
  if(pBuf>m_Data+CSM_BUF_COUNT-10)
  pBuf=m_Data;
  m_Data[CSM_BUF_COUNT]=0;
  pDlg->m_pMutex->Lock();
  sprintf(pDlg->m_sBufCSM.GetBuffer(CSM_BUF_COUNT+10),m_Data);
  pDlg->m_pMutex->Unlock();
  if (bSleep)
  Sleep(100);
  pDlg->m_pSemaphoreEmpty ->Unlock();
  }

  return CWinThread::Run();
  }
  ///////////////////////////////////////
  3启动线程:
  ///////////////////////////////////////
  m_pSemaphoreFull =new CSemaphore(1,1);
  m_pSemaphoreEmpty =new CSemaphore(0,1);
  m_pMutex =new CMutex;
  this->m_bUpdateAuto =false;

  this->m_pProducerThread =new CProducerThread(this);
  this->m_pConsumerThread =new CConsumerThread(this);
  this->m_sBuf.Format("1234567890");
  this->UpdateData(false);
  this->m_pProducerThread->CreateThread(CREATE_SUSPENDED);
  VERIFY(m_pProducerThread->SetThreadPriority(THREAD_PRIORITY_IDLE));
  this->m_pConsumerThread->CreateThread(CREATE_SUSPENDED);
  VERIFY(m_pConsumerThread->SetThreadPriority(THREAD_PRIORITY_IDLE));
  this->m_pProducerThread->ResumeThread();
  this->m_pConsumerThread->ResumeThread();

键盘也能翻页,试试“← →”键

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品