码迷,mamicode.com
首页 > 其他好文 > 详细

读者写着问题

时间:2014-06-26 10:41:57      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   ext   2014   问题   

读者优先

//读者写者问题-读者优先
#include<stdio.h>
#include<process.h>
#include<windows.h>
using namespace std;
HANDLE rmutex,wmutex;
int Readcount=0;
int read_num=10,write_num=5;
DWORD WINAPI read(LPVOID lpParam)
{
        WaitForSingleObject(rmutex,INFINITE);
        if(Readcount==0)
        {
            WaitForSingleObject(wmutex,INFINITE);
            printf("读者 %s-------------------------对wmutex已加锁\n",lpParam);
        }
        Readcount++;
        ReleaseMutex(rmutex);
        printf("读者 %s 开始读操作\n",lpParam);
        for(int i=0;i<5;i++)
        {
            Sleep(1);
            printf("读者 %s 正在读\n");
        }
        printf("读者 %s 读操作完毕\n",lpParam);
        WaitForSingleObject(rmutex,INFINITE);
        Readcount--;
        if(Readcount==0)
        {
            ReleaseMutex(wmutex);
            printf("读者 %s-------------------------对wmutex已解锁\n",lpParam);
        }
        ReleaseMutex(rmutex);
        printf("读者 %s ******死亡\n",lpParam);
        //Sleep(10000);
        while(true)
        {
            Sleep(100);
            if(Readcount==0)
            {
                ReleaseMutex(wmutex);
                break;
            }
        }
        Sleep(10000);
}
DWORD WINAPI write(LPVOID lpParam)
{
        WaitForSingleObject(wmutex,INFINITE);
        printf("写者 %s-------------------------对wmutex已加锁\n",lpParam);
        printf("写者 %s 开始写操作\n",lpParam);
        for(int i=0;i<5;i++)
        {
            Sleep(1);
            printf("写者 %s 正在写\n");
        }
        printf("写者 %s 写操作完毕\n",lpParam);
        ReleaseMutex(wmutex);
        printf("写者 %s-------------------------对wmutext已解锁\n",lpParam);
        printf("写者 %s ******死亡\n");
        Sleep(10000);
}
int main()
{
    rmutex=CreateMutex(NULL,false,NULL);
    wmutex=CreateMutex(NULL,false,NULL);
    char *r[10]={"a","b","c","d","e","f","g","h","i","j"};
    char *w[5]={"a","b","c","d","e"};
    for(int i=0;i<read_num;i++)
    {
        printf("读者 %s 创建并申请读\n",r[i]);
        CreateThread(NULL,0,read,r[i],0,NULL);
        if(i>=5)
        {
            printf("写者 %s 创建并申请写\n",w[i-5]);
            CreateThread(NULL,0,write,w[i-5],0,NULL);
        }
    }
    Sleep(10000);
    return 0;
}



写者优先

//读者写者问题-写者优先
#include<stdio.h>
#include<windows.h>
using namespace std;
HANDLE rmutex,wmutex,rwmutex;
int Readcount=0;
int read_num=10,write_num=5;
DWORD WINAPI read(LPVOID lpParam)
{
    WaitForSingleObject(rwmutex,INFINITE);
    ReleaseMutex(rwmutex);
    WaitForSingleObject(rmutex,INFINITE);
    if(Readcount==0)
    {
        WaitForSingleObject(wmutex,INFINITE);
        printf("读者 %s-------------------------对wmutex已加锁\n",lpParam);
    }
    Readcount++;
    ReleaseMutex(rmutex);
    //read操作
    printf("读者 %s 开始读操作\n",lpParam);
    for(int i=0; i<5; i++)
    {
        Sleep(1);
        printf("读者 %s 正在读\n");
    }
    printf("读者 %s 读操作完毕\n",lpParam);
    WaitForSingleObject(rmutex,INFINITE);
    Readcount--;
    if(Readcount==0)
    {
        ReleaseMutex(wmutex);
        printf("读者 %s-------------------------对wmutex已解锁\n",lpParam);
    }
    ReleaseMutex(rmutex);
    printf("读者 %s ******死亡\n",lpParam);
    while(true)
    {
        Sleep(100);
        if(Readcount==0)
        {
            ReleaseMutex(wmutex);
            break;
        }
    }
    Sleep(10000);
}
DWORD WINAPI write(LPVOID lpParam)
{
    WaitForSingleObject(rwmutex,INFINITE);
    WaitForSingleObject(wmutex,INFINITE);
    printf("写者 %s-------------------------对wmutex已加锁\n",lpParam);
    printf("写者 %s 开始写操作\n",lpParam);
    for(int i=0; i<5; i++)
    {
        Sleep(1);
        printf("写者 %s 正在写\n");
    }
    printf("写者 %s 写操作完毕\n",lpParam);
    ReleaseMutex(wmutex);
    printf("写者 %s-------------------------对wmutext已解锁\n",lpParam);
    printf("写者 %s ******死亡\n");
    ReleaseMutex(rwmutex);
    Sleep(10000);
}
int main()
{
    rmutex=CreateMutex(NULL,false,NULL);
    wmutex=CreateMutex(NULL,false,NULL);
    rwmutex=CreateMutex(NULL,false,NULL);
    char *r[10]= {"a","b","c","d","e","f","g","h","i","j"};
    char *w[5]= {"a","b","c","d","e"};
    for(int i=0; i<read_num; i++)
    {
        printf("读者 %s 创建并申请读\n",r[i]);
        CreateThread(NULL,0,read,r[i],0,NULL);
        if(i>=5)
        {
            printf("写者 %s 创建并申请写\n",w[i-5]);
            CreateThread(NULL,0,write,w[i-5],0,NULL);
        }
    }
    Sleep(10000);
    return 0;
}


读者写着问题,布布扣,bubuko.com

读者写着问题

标签:class   blog   code   ext   2014   问题   

原文地址:http://blog.csdn.net/crescent__moon/article/details/34541097

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!