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
首页 上一页 3 4 5 6 7 8 下一页 尾页 6/8/8