自己写了一个简单的,不知道有没有错误,诚恳各位指点。#include <iostream.h>
#include <windows.h>
//读者为主线程,写者为主线程创建的子线程
//为读者优先策略
DWORD WINAPI ThreadWriProc(
LPVOID lpParameter   // thread data
);
static HANDLE hThreadWri;
static HANDLE hWriMutex;
static HANDLE hCouMutex;
int count=0;
int main()
{
//创建线程
hThreadWri=CreateThread(NULL,0,ThreadWriProc,NULL,0,NULL);
CloseHandle(hThreadWri);
//创建互斥对象
hWriMutex=CreateMutex(NULL,FALSE,"WriMutex");
hCouMutex=CreateMutex(NULL,FALSE,"CouMutex");
//读者程序执行主体
while(TRUE)
{
Sleep(8000);
WaitForSingleObject(hCouMutex,INFINITE);
++count;
if (1==count)
{
WaitForSingleObject(hWriMutex,INFINITE);
}
ReleaseMutex(hCouMutex);
cout<<"读者正在读程序!"<<endl;
Sleep(1000);
WaitForSingleObject(hCouMutex,INFINITE);
--count;
if (0==count)
{
ReleaseMutex(hWriMutex);
Sleep(1000);
}
ReleaseMutex(hCouMutex);
}
return 0;
}
DWORD WINAPI ThreadWriProc(
   LPVOID lpParameter   // thread data
)
{
WaitForSingleObject(hWriMutex,INFINITE);
cout<<"写者正在写程序!"<<endl;
Sleep(1000);
ReleaseMutex(hWriMutex);
return 1;
}一个高手写的,基本上看不懂:
#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 100
#define MAX_THREAD_NUM 64
#define MAX_FILE_NUM 32
#define MAX_STR_LEN 32
int readcount=0;
int writecount=0;
CRITICAL_SECTION RP_Write;
CRITICAL_SECTION WP_Read;
CRITICAL_SECTION cs_Write;
CRITICAL_SECTION cs_Read;
struct ThreadInfo
{ int serial;
char entity;
double delay;
double persist;
};
void ReaderPriority(char* file);
void RP_ReaderThread(void *p);
void RP_WriterThread(void *p);
void WriterPriority(char* file);
void WP_ReaderThread(void *p);
void WP_WriterThread(void *p);
int main()
{
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)\n");
do
{
ch=(char)_getch();
}while(ch!='1'&&ch!='2'&&ch!='3');
system("cls");
if(ch=='3')
return 0;
else if(ch=='1')
ReaderPriority("thread.dat");
else
WriterPriority("thread.dat");
printf("\nPress Any key to continue.\n");
getch();
system("cls");
}
return 0;
}void ReaderPriority(char* file)
{
DWORD 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; 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;
inFile>>thread_info[n_thread].persist;
n_thread++;
inFile.get();
}
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");
}
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 file.\n",m_serial);
Sleep(m_persist);
printf("Writer thread %d finished writing file.\n",m_serial);
LeaveCriticalSection(&RP_Write);
}
void WriterPriority(char* file){
DWORD n_thread=0;
DWORD thread_ID;
DWORD wait_for_all;
HANDLE h_Mutex;
h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_writecount");
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
writecount=0;
InitializeCriticalSection(&WP_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;
n_thread++;
inFile.get();
}
for(int i=0;i<(int)(n_thread);i++)
{
if(thread_info[i].entity==WRITER||thread_info[i].entity=='w')
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_info[i],0,&thread_ID);
else
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),&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");
}void WP_WriterThread(void *p)
{
HANDLE h_Mutex;
h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_writecount");
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("Writer thread %d sents the writing require.\n",m_serial);
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
writecount++;
if(writecount==1)
EnterCriticalSection(&WP_Read);
ReleaseMutex(h_Mutex);
printf("Writer thread %d begins to write file.\n",m_serial);
Sleep(m_persist);
printf("Writer thread %d finished writing file.\n",m_serial);
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
writecount--;
if(writecount==0)
LeaveCriticalSection(&WP_Read);
ReleaseMutex(h_Mutex);
}
void WP_ReaderThread(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("Reader thread %d sents the Reading require.\n",m_serial);
EnterCriticalSection(&WP_Read);
printf("Reader thread %d begins to read file.\n",m_serial);
Sleep(m_persist);
printf("Reader thread %d finished reading file.\n",m_serial);
LeaveCriticalSection(&WP_Read);
}各位也写点自己的多线程同步问题中的经典问题:读者-写者问题吧。