计算机应用 | 古代文学 | 市场营销 | 生命科学 | 交通物流 | 财务管理 | 历史学 | 毕业 | 哲学 | 政治 | 财税 | 经济 | 金融 | 审计 | 法学 | 护理学 | 国际经济与贸易
计算机软件 | 新闻传播 | 电子商务 | 土木工程 | 临床医学 | 旅游管理 | 建筑学 | 文学 | 化学 | 数学 | 物理 | 地理 | 理工 | 生命 | 文化 | 企业管理 | 电子信息工程
计算机网络 | 语言文学 | 信息安全 | 工程力学 | 工商管理 | 经济管理 | 计算机 | 机电 | 材料 | 医学 | 药学 | 会计 | 硕士 | 法律 | MBA
现当代文学 | 英美文学 | 通讯工程 | 网络工程 | 行政管理 | 公共管理 | 自动化 | 艺术 | 音乐 | 舞蹈 | 美术 | 本科 | 教育 | 英语 |

基于C++的读者与写者问题read—write problem的实现(7)

2013-07-08 01:38
导读:sp; //修改写者数目 if(writecount==1) { EnterCriticalSection(cs_Read); } ReleaseMutex(h_Mutex3); EnterCriticalSection(cs_Write); printf("Writer thread %d begins to write to the file.\n",m_serial)
sp;  //修改写者数目
 if(writecount==1)
 {
  EnterCriticalSection(&cs_Read);
 }
 ReleaseMutex(h_Mutex3);
 EnterCriticalSection(&cs_Write);
 printf("Writer thread %d begins to write to the file.\n",m_serial);
 Sleep(m_persist);

 printf("Writer thread %d finished writing to the file.\n",m_serial);
 LeaveCriticalSection(&cs_Write);

 wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);
 writecount--;
 if(writecount==0)
 {
  LeaveCriticalSection(&cs_Read);
 }
ReleaseMutex(h_Mutex3);
}
/////////////////////////////////////////////
//写者优先处理函数
// file:文件名

void WriterPriority(char * file)
{
 DWORD n_thread=0;
 DWORD thread_ID;
 DWORD wait_for_all;


 HANDLE h_Mutex1;
 h_Mutex1=CreateMutex(NULL,FALSE,"mutex1");
 HANDLE h_Mutex2;
 h_Mutex2=CreateMutex(NULL,FALSE,"mutex2");
 HANDLE h_Mutex3;
 h_Mutex3=CreateMutex(NULL,FALSE,"mutex3");
 HANDLE h_Thread[MAX_THREAD_NUM];
 ThreadInfo thread_info[MAX_THREAD_NUM];

 readcount=0;
 writecount=0;
 InitializeCriticalSection(&cs_Write);
 InitializeCriticalSection(&cs_Read);
 
 ifstream inFile;
 inFile.open (file);
 printf("Writer priority:\n\n");
 while(inFile)
 {
  inFile>>thread_info[n_thread].serial;
  inFile>>thread_info[n_thread].entity;
  inFile>>thread_info[n_thread].delay;
  inFile>>thread_info[n_thread++].persist;
  inFile.get();
 }
 for(int i=0;i<(int)(n_thread);i++)
 { (科教范文网http://fw.ΝsΕΑc.com编辑)
  if(thread_info[i].entity==READER||thread_info[i].entity =='r')
  {
   //创建读者进程
 h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),&thread_info[i],0,&thread_ID);
  }
  else
  {
   //创建写线程
   h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_info[i],0,&thread_ID);
  }

 }
 //等待所有的线程结束
 wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
 printf("All reader and writer have finished operating.\n");
}

/////////////////////////////////////////////////////
//主函数
int main(int argc,char *argv[])
{
 char ch;
 while(true)
 {
  printf("*************************************\n");
  printf("   1.Reader Priority\n");
  printf("   2.Writer Priority\n");
  printf("   3.Exit to Windows\n");
  printf("*************************************\n");
  printf("Enter your choice(1,2,3): ");
  do{
   ch=(char)_getch();
  }while(ch!='1'&&ch!='2'&&ch!='3');
  system("cls");
  if(ch=='3')
   return 0;
  else if(ch=='1')
  &nb

上一篇:C# 考务和成绩管理系统(一)毕业论文 下一篇:没有了