#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "stdlib.h"
#include "fstream.h"
#include "io.h"
#include "string.h"
#include "stdio.h"#define READER 'R' //读者
#define WRITER 'W' //写者
#define INTE_PER_SEC 1000
#define MAX_THREAD_NUM 64
#define MAX_FILE_NUM 32
#define MAX_STR_LEN 32int readcount=0; //读者数目
int writecount=0; //写者数目//临界区
CRITICAL_SECTION RP_Write; //读者优先临界区
CRITICAL_SECTION cs_Write; //写者临界区
CRITICAL_SECTION cs_Read; //读者临界区struct ThreadInfo //线程信息
{
int serial;
char entity;
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); 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);
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
readcount--;
if(readcount==0)
{
LeaveCriticalSection(&RP_Write); //所有读者读完,唤醒写者
}
ReleaseMutex(h_Mutex);
}//读者优先——写者线程
void RP_WriterThread(void* p)
{
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("Writer thread %d sents the writing require.\n",m_serial);
EnterCriticalSection(&RP_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(&RP_Write); //释放资源
}//读者优先处理
//file:文件名
void ReaderPriority(char* file)
{
int n_thread=0;
DWORD thread_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;
InitializeCriticalSection(&RP_Write); //初始化临界区
ifstream inFile(file); //根据文件内容,创建读、写者线程
if(!inFile)
{
cout<<"File cannot be opened."<<endl;
return;
}
printf("Reader Priority:\n\n");
int c=11;
while(c!=EOF)
{
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread++].persist;
c=inFile.get();
}
inFile.close();
for(int i=0;i<(int)(n_thread);i++)
{
if(thread_info[i].entity==READER||thread_info[i].entity=='r')
{//创建读者线程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReaderThread),&thread_info[i],0,&thread_ID);
}
else
{ //创建写者线程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_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");
}
#include "windows.h"
#include "conio.h"
#include "stdlib.h"
#include "fstream.h"
#include "io.h"
#include "string.h"
#include "stdio.h"#define READER 'R' //读者
#define WRITER 'W' //写者
#define INTE_PER_SEC 1000
#define MAX_THREAD_NUM 64
#define MAX_FILE_NUM 32
#define MAX_STR_LEN 32int readcount=0; //读者数目
int writecount=0; //写者数目//临界区
CRITICAL_SECTION RP_Write; //读者优先临界区
CRITICAL_SECTION cs_Write; //写者临界区
CRITICAL_SECTION cs_Read; //读者临界区struct ThreadInfo //线程信息
{
int serial;
char entity;
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); 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);
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
readcount--;
if(readcount==0)
{
LeaveCriticalSection(&RP_Write); //所有读者读完,唤醒写者
}
ReleaseMutex(h_Mutex);
}//读者优先——写者线程
void RP_WriterThread(void* p)
{
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("Writer thread %d sents the writing require.\n",m_serial);
EnterCriticalSection(&RP_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(&RP_Write); //释放资源
}//读者优先处理
//file:文件名
void ReaderPriority(char* file)
{
int n_thread=0;
DWORD thread_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;
InitializeCriticalSection(&RP_Write); //初始化临界区
ifstream inFile(file); //根据文件内容,创建读、写者线程
if(!inFile)
{
cout<<"File cannot be opened."<<endl;
return;
}
printf("Reader Priority:\n\n");
int c=11;
while(c!=EOF)
{
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread++].persist;
c=inFile.get();
}
inFile.close();
for(int i=0;i<(int)(n_thread);i++)
{
if(thread_info[i].entity==READER||thread_info[i].entity=='r')
{//创建读者线程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReaderThread),&thread_info[i],0,&thread_ID);
}
else
{ //创建写者线程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_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");
}
解决方案 »
- window media player 控件使用问题
- 怎样能让label控件显示黑色背景,白色文字
- Unhandled exception in Mby.exe (MFC42D.DLL) :0xC0000005;Access Violation 怎么回事?
- CIPAddressCtrl改变大小后,小圆点位置不正确的问题
- VC循环中每步的时间怎么控制
- 如何刷新文件夹视图以反映扩展名显隐的改变
- CListCtrl中某列都为 进度条(急!)
- 一个关于使用visul assist 6.0的问题,怎么样用它改变编辑框的背景颜色??
- 贪玩的孩子,找不到回家的路!
- 在WIN2000中如何修改注册表??
- vc如何设置IE控件的cookie? (免去用户手工登陆)
- 靠!贴子不准提前?难道3天才能提前一次吗?要是这样以后就不来这里提问了!
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;
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); //有写者在等待,读者阻塞
wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);
readcount++;
if(readcount==1){
EnterCriticalSection(&cs_Write); //第一个读者,等待写者写完
}
ReleaseMutex(h_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);
wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);
readcount--;
if(readcount==0)
{
LeaveCriticalSection(&cs_Write);
}
ReleaseMutex(h_Mutex2);
}//写者优先——写者线程
void WP_WriterThread(void* p)
{
DWORD m_delay;
DWORD m_persist;
int m_serial; DWORD wait_for_mutex3;
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 writing require.\n",m_serial);
wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);
writecount++;
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);
}//写者优先处理
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(file); //根据文件内容,创建读、写者线程
if(!inFile)
{
cout<<"File cannot be opened."<<endl;
return;
}
printf("Writer Priority:\n\n");
int c=11;
while(c!=EOF)
{
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread++].persist;
c=inFile.get();
}
inFile.close();
for(int i=0;i<(int)(n_thread);i++)
{
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 or 3): ");
do{
ch=(char)_getch();
}while(ch!='1' && ch!='2' && ch!='3');
system("cls");
if(ch=='3')
return 0;
else {
if(ch=='1'){ //读者优先读者-写者问题
ReaderPriority("thread.txt");}
else{ //写者优先读者-写者问题
WriterPriority("thread.txt");}
}
printf("\nPress Any Key To Continue:");
_getch();
system("cls");
}
return 0;
}程序应该是没有问题。可是用VC编译的时候报出
c:\program files\microsoft visual studio\myprojects\2\2.cpp(1) : fatal error C1083: Cannot open precompiled header file: 'Debug/2.pch': No such file or directory不知道为什么。。急
在project->setting的c/c++属性页precompiled header中去掉预编译试试