可以描述一个资源有多少。
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
using namespace std;
CRITICAL_SECTION cs;
typedef struct{
HANDLE h1;
HANDLE h2;
HANDLE h3;
HANDLE h4;
int a;
}PARAMS, *PPARAMS;
void read1(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
WaitForSingleObject(pparams->h2, INFINITE);
EnterCriticalSection(&cs); // 临界区(代码块锁,只能有一个线程访问)
cout<<"读线程1开始读取...\n";
cout<<(pparams->a)<<endl;
LeaveCriticalSection(&cs);
Sleep(1000);
ReleaseSemaphore(pparams->h1, 1, NULL); // 写信号量++
}
}
void read2(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
WaitForSingleObject(pparams->h3, INFINITE);
EnterCriticalSection(&cs); // 临界区(代码块锁,只能有一个线程访问)
cout<<"读线程2开始读取...\n";
cout<<(pparams->a)<<endl;
LeaveCriticalSection(&cs);
Sleep(1000);
ReleaseSemaphore(pparams->h1, 1, NULL); // 写信号量++
}
}
void read3(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
WaitForSingleObject(pparams->h4, INFINITE);
EnterCriticalSection(&cs); // 临界区(代码块锁,只能有一个线程访问)
cout<<"读线程3开始读取...\n";
cout<<(pparams->a)<<endl;
LeaveCriticalSection(&cs);
Sleep(1000);
ReleaseSemaphore(pparams->h1, 1, NULL); // 写信号量++
}
}
void write(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
// 只有写信号量有3个的时候才能走完这3个wait函数
WaitForSingleObject(pparams->h1, INFINITE);
WaitForSingleObject(pparams->h1, INFINITE);
WaitForSingleObject(pparams->h1, INFINITE);
cout << "=================\n";
cout << "写线程开始写入...\n";
pparams->a = rand() % 256;
cout << "写入"<< (pparams->a) <<endl;
ReleaseSemaphore(pparams->h2, 1, NULL); // 读信号量++
ReleaseSemaphore(pparams->h3, 1, NULL); // 读信号量++
ReleaseSemaphore(pparams->h4, 1, NULL); // 读信号量++
}
}
int main()
{
PARAMS params;
params.h1 = CreateSemaphore(NULL,
3, // 刚开始有多少信号量
3, // 最多可以有多少信号量
NULL); // 信号量名称
params.h2 = CreateSemaphore(NULL, 0, 1, NULL);
params.h3 = CreateSemaphore(NULL, 0, 1, NULL);
params.h4 = CreateSemaphore(NULL, 0, 1, NULL);
InitializeCriticalSection(&cs); // 初始化临界区
_beginthread(read1, 0, ¶ms);
_beginthread(read2, 0, ¶ms);
_beginthread(read3, 0, ¶ms);
_beginthread(write, 0, ¶ms);
int a;
cin >> a; // 主线程阻塞用
DeleteCriticalSection(&cs); // 释放临界区
return 0;
}
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
using namespace std;
CRITICAL_SECTION cs;
typedef struct{
HANDLE h1;
HANDLE h2;
HANDLE h3;
HANDLE h4;
int a;
}PARAMS, *PPARAMS;
void read1(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
WaitForSingleObject(pparams->h2, INFINITE);
EnterCriticalSection(&cs); // 临界区(代码块锁,只能有一个线程访问)
cout<<"读线程1开始读取...\n";
cout<<(pparams->a)<<endl;
LeaveCriticalSection(&cs);
Sleep(1000);
ReleaseSemaphore(pparams->h1, 1, NULL); // 写信号量++
}
}
void read2(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
WaitForSingleObject(pparams->h3, INFINITE);
EnterCriticalSection(&cs); // 临界区(代码块锁,只能有一个线程访问)
cout<<"读线程2开始读取...\n";
cout<<(pparams->a)<<endl;
LeaveCriticalSection(&cs);
Sleep(1000);
ReleaseSemaphore(pparams->h1, 1, NULL); // 写信号量++
}
}
void read3(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
WaitForSingleObject(pparams->h4, INFINITE);
EnterCriticalSection(&cs); // 临界区(代码块锁,只能有一个线程访问)
cout<<"读线程3开始读取...\n";
cout<<(pparams->a)<<endl;
LeaveCriticalSection(&cs);
Sleep(1000);
ReleaseSemaphore(pparams->h1, 1, NULL); // 写信号量++
}
}
void write(PVOID pvoid){
while(TRUE)
{
volatile PPARAMS pparams = (PPARAMS)pvoid;
// 只有写信号量有3个的时候才能走完这3个wait函数
WaitForSingleObject(pparams->h1, INFINITE);
WaitForSingleObject(pparams->h1, INFINITE);
WaitForSingleObject(pparams->h1, INFINITE);
cout << "=================\n";
cout << "写线程开始写入...\n";
pparams->a = rand() % 256;
cout << "写入"<< (pparams->a) <<endl;
ReleaseSemaphore(pparams->h2, 1, NULL); // 读信号量++
ReleaseSemaphore(pparams->h3, 1, NULL); // 读信号量++
ReleaseSemaphore(pparams->h4, 1, NULL); // 读信号量++
}
}
int main()
{
PARAMS params;
params.h1 = CreateSemaphore(NULL,
3, // 刚开始有多少信号量
3, // 最多可以有多少信号量
NULL); // 信号量名称
params.h2 = CreateSemaphore(NULL, 0, 1, NULL);
params.h3 = CreateSemaphore(NULL, 0, 1, NULL);
params.h4 = CreateSemaphore(NULL, 0, 1, NULL);
InitializeCriticalSection(&cs); // 初始化临界区
_beginthread(read1, 0, ¶ms);
_beginthread(read2, 0, ¶ms);
_beginthread(read3, 0, ¶ms);
_beginthread(write, 0, ¶ms);
int a;
cin >> a; // 主线程阻塞用
DeleteCriticalSection(&cs); // 释放临界区
return 0;
}