天天看点

win32 pv操作 读者写者问题

p操作对应着WaitForSingleObject

v操作对应着ReleaseSemaphore

CRITICAL_SECTION就是临界区,你懂的

写者写一次,读者read1,read2,read3都读一次之后,write才能再次写。一下是win32源代码。例子比较简单,不过网上现成的例子貌似不太多,要有也是很杂乱的那种,所以写下来

#include<windows.h>
#include<process.h>
#include<iostream>
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;
    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);

//    HANDLE hEvent;
//    hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
//    WaitForSingleObject(hEvent,INFINITE);

    int a;cin>>a;

    DeleteCriticalSection(&cs);

    return 0;
}
           

继续阅读