论文首页哲学论文经济论文法学论文教育论文文学论文历史论文理学论文工学论文医学论文管理论文艺术论文 |
//退出线程
printf("Write thread %d finished writing to the file.\n",m_serial);
//释放资源
LeaveCriticalSection(&RP_Write);
}
//////////////////////////////////////////////////////////////
//读者优先处理函数
//file:文件名
void ReaderPriority(char *file)
{
DWORD n_thread=0; //线程数目
DWORD thread_ID; //线程ID
DWORD wait_for_all; //等待所有线程结束
//互斥对象
HANDLE h_Mutex;
h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");
//线程对象的数组
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
readcount=0; //初始化readcount
InitializeCriticalSection(&RP_Write); //初始化临界区
ifstream inFile;
inFile.open (file);
printf("Reader Priority:\n\n");
while(inFile)
{
//读入每一个读者,写者的信息
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
(科教作文网http://zw.ΝsΕAc.Com编辑整理)
}
//等待所有的线程结束
wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
printf("All reader and writer have finished operating.\n");
}
////////////////////////////////////////////////////////
//写者优先---读者线程
//P:读者线程信息
void WP_ReaderThread(void *p)
{
//互斥变量
HANDLE h_Mutex1;
h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex1");
HANDLE h_Mutex2;
h_Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex2");
DWORD wait_for_mutex1; //等待互斥变量所有权
DWORD wait_for_mutex2;
DWORD m_delay;