基于C++的读者与写者问题read—write problem的实现(一)(5)
作者:佚名; 更新时间:2019-07-20

;  //延迟时间
 DWORD m_persist;                   //读文件持续时间
 int m_serial;                      //线程的序号
 //从参数中得到信息
 m_serial=((ThreadInfo*)(p))->serial ;

m_delay=(DWORD)(((ThreadInfo*)(p))->delay *INTE_PER_SEC);
 m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
 Sleep(m_delay);                  //延迟等待

printf("Reader thread %d sents the reading require.\n",m_serial);
 wait_for_mutex1=WaitForSingleObject(h_Mutex1,-1);

//读者进去临界区
 EnterCriticalSection(&cs_Read);

//阻塞互斥对象Mutex2,保证对readCount的访问和修改互斥
  wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);
  //修改读者的数目
  readcount++;
  if(readcount==1)
  {
   // 如果是第1个读者,等待写者写完
   EnterCriticalSection(&cs_Write);
  }
  ReleaseMutex(h_Mutex2);// 释放互斥信号 Mutex2
  //让其他读者进去临界区
  LeaveCriticalSection(&cs_Read);
  ReleaseMutex(h_Mutex1);
  //读文件
  printf("Reader thread %d begins to read file.\n",m_serial);
  Sleep(m_persist);

//退出线程
   printf("Reader thread %d finished reading  file.\n",m_serial);
   //阻塞互斥对象Mutex2,保证对readcount的访问,修改互斥
   wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);
   readcount--;
   if(readcount==0)
   {
    //最后一个读者,唤醒写者
    LeaveCriticalSection(&cs_Write);
   }
   ReleaseMutex(h_Mutex2);  //释放互斥信号
}
///////////////////////////////////////////
//写者优先---写者线程
//P:写者线程信息


void WP_WriterThread(void *p)
{
 DWORD wait_for_mutex3;            //互斥变量
 DWORD m_delay;                   //延迟时间
 DWORD m_persist;                 //读文件持续时间
 int m_serial;                    //线程序号

HANDLE h_Mutex3;
 h_Mutex3=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex3");

//从参数中获得信息
 m_serial=((ThreadInfo*)(p))->serial ;
 m_delay=(DWORD)(((ThreadInfo*)(p))->delay *INTE_PER_SEC);
 m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
 Sleep(m_delay);                  //延迟等待

printf("Writer thread %d sents the reading require.\n",m_serial);
 wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);
 writecount++;            &nb

核心期刊快速发表
Copyright@2000-2030 论文期刊网 Corporation All Rights Reserved.
《中华人民共和国信息产业部》备案号:ICP备07016076号;《公安部》备案号:33010402003207
本网站专业、正规提供职称论文发表和写作指导服务,并收录了海量免费论文和数百个经国家新闻出版总署审批过的具有国内统一CN刊号与国际标准ISSN刊号的合作期刊,供诸位正确选择和阅读参考,免费论文版权归原作者所有,谨防侵权。联系邮箱:256081@163.com