nbsp; //线程类别(判断是读者还是写者线程)
double delay; //线程延迟时间
double persist; //线程读写操作时间
};
///////////////////////////////////////////////////////////////////////////
// 读者优先---读者线程
//P:读者线程信息
void RP_ReaderThread(void *p)
{
//互斥变量
HANDLE h_Mutex;
h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
DWORD wait_for_mutex; //等待互斥变量所有权
DWORD m_delay; //延迟时间
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);
//等待互斥信号,保证对ReadCount 的访问,修改互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
//读者数目增加
readcount++;
if(readcount==1)
{
//第一个读者,等待资源
EnterCriticalSection(&RP_Write);
}
ReleaseMutex(h_Mutex); //释放互斥信号
//读文件
printf("Reader thread %d begins to read file.\n",m_serial);
Sleep(m_persist);
//退出线程
printf("Reader thread %d finished reading file.\n",m_serial);
//等待互斥信号,保证对ReadCount的访问,修改互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
//读者数目减少
readcount--;
if(readcount==0)
{
//如果所有的读者读完,唤醒写者
LeaveCriticalSection(&RP_Write);
}
ReleaseMutex(h_Mutex); //释放互斥信号
}
//////////////////////////////////////////////////////////////
//P:写者线程信息
void RP_WriterThread(void *p)
{
DWORD m_delay; //延迟时间
DWORD m_persist; //写文件持续时间
int m_serial; //线程序号
// 从参数中获得信息
m_serial=((ThreadInfo*)(p))->serial ;
m_delay=(