| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 540 人关注过本帖
标题:麻烦大家帮我看看消费者与生产者,我的程序哪出了问题
只看楼主 加入收藏
lonely_21
Rank: 5Rank: 5
等 级:职业侠客
威 望:3
帖 子:108
专家分:395
注 册:2011-11-13
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:3 
麻烦大家帮我看看消费者与生产者,我的程序哪出了问题
#include <windows.h>
#include <iostream>
using namespace std;
#include <malloc.h>
#define BUFFERSIZE 8

template<class T>
class Queue
{
    T *data;
    int front,rear;
    int curlen;
public:
    Queue():front(0),rear(0),curlen(0)
    {
        data = (T*)malloc(sizeof(T)*BUFFERSIZE);
    }
    void EnQue(T x)
    {
        if(isfull()) return;
        data[rear++] = x;
        rear = rear%8;
        curlen++;
    }
    void DeQueue(T &x)
    {
        if(isempty()) return;
        x=data[front];
        front = (front+1)%8;
        curlen--;
    }
    T GetTop()
    {

    }
    bool isempty()
    {
        return (curlen == 0);
    }
    bool isfull()
    {
        return (curlen == 8);
    }
    ~Queue()
    {
        if(data != NULL)
        {
            free(data);
            data = NULL;
            curlen = front = rear = 0;
        }

    }
};
Queue<int> qu;
HANDLE pro;
HANDLE con;
HANDLE s;


DWORD _stdcall producer(LPVOID)
{
    for(int i=0; i < 20; i++)
    {
        WaitForSingleObject(s,-1);
        if(qu.isfull())
        {
            WaitForSingleObject(pro,-1);         //等待pro变为大于0
        }
        qu.EnQue(i);
        printf("生产 %d\n",i);
        ReleaseSemaphore(con,1,NULL);
        ReleaseSemaphore(s,1,NULL);

    }
    WaitForSingleObject(s,-1);
    qu.EnQue(-1);
    printf("生产结束\n");
    ReleaseSemaphore(con,1,NULL);
    ReleaseSemaphore(s,1,NULL);
    return 0;
}
DWORD _stdcall consumer(LPVOID)
{
    int x;
    for(;;)
    {
        WaitForSingleObject(s,-1);
        if(qu.isempty())
        {
            WaitForSingleObject(con,-1);                     //等待con变为大于0
        }
        qu.DeQueue(x);
        if(x == -1)
            break;
        printf("消费 %d\n",x);
        ReleaseSemaphore(pro,1,NULL);
        ReleaseSemaphore(s,1,NULL);
        
    }
    printf("消费结束\n");
    ReleaseSemaphore(s,1,NULL);
    return 0;
}
int main()
{
    pro = CreateSemaphore(NULL,1,1,NULL);
    con = CreateSemaphore(NULL,0,1,NULL);
    s   = CreateSemaphore(NULL,2,2,NULL);
    HANDLE hThrds[2];
    DWORD id;
    hThrds[0] = CreateThread(NULL,0,producer,NULL,-1,&id);
    hThrds[1] = CreateThread(NULL,0,consumer,NULL,-1,&id);
    WaitForMultipleObjects(2,hThrds,true,-1);
    CloseHandle(hThrds[0]);
    CloseHandle(hThrds[1]);
    return 0;
}
搜索更多相关主题的帖子: include void public 消费者 
2012-07-16 18:55
lonely_21
Rank: 5Rank: 5
等 级:职业侠客
威 望:3
帖 子:108
专家分:395
注 册:2011-11-13
收藏
得分:0 
#include <windows.h>
#include <iostream>
using namespace std;
#include <malloc.h>
#define BUFFERSIZE 8

template<class T>
class Queue
{
    T *data;
    int front,rear;
    int curlen;
public:
    Queue():front(0),rear(0),curlen(0)
    {
        data = (T*)malloc(sizeof(T)*BUFFERSIZE);
    }
    void EnQue(T x)
    {
        if(isfull()) return;
        data[rear++] = x;
        rear = rear%8;
        curlen++;
    }
    void DeQueue(T &x)
    {
        if(isempty()) return;
        x=data[front];
        front = (front+1)%8;
        curlen--;
    }
    T GetTop()
    {

    }
    bool isempty()
    {
        return (curlen == 0);
    }
    bool isfull()
    {
        return (curlen == 8);
    }
    ~Queue()
    {
        if(data != NULL)
        {
            free(data);
            data = NULL;
            curlen = front = rear = 0;
        }

    }
};
Queue<int> qu;
HANDLE hMutex;
HANDLE isEmpty;
HANDLE isFull;


DWORD _stdcall producer(LPVOID)
{
    for(int i=0; i < 20; i++)
    {
        WaitForSingleObject(isFull,-1);
        WaitForSingleObject(hMutex,-1);
        qu.EnQue(i);
        printf("producer : %d\n",i);
        Sleep(1500);
        ReleaseMutex(hMutex);
        ReleaseSemaphore(isEmpty,1,NULL);        

    }

    printf("生产结束\n");
    return 0;
}
DWORD _stdcall consumer(LPVOID)
{
    int x;
    for(;;)
    {
        WaitForSingleObject(isEmpty,-1);
        WaitForSingleObject(hMutex,-1);
        qu.DeQueue(x);
        printf("producer : %d\n",x);
        Sleep(1500);
        ReleaseMutex(hMutex);
        ReleaseSemaphore(isFull,1,NULL);
   
    }
    printf("消费结束\n");
    return 0;
}
int main()
{
    isEmpty = CreateSemaphore(NULL,0,BUFFERSIZE-1,NULL);
    isFull  = CreateSemaphore(NULL,BUFFERSIZE,BUFFERSIZE,NULL);
    hMutex  = CreateMutex(NULL,false,NULL);
    HANDLE hThrds[2];
    DWORD id;
    hThrds[0] = CreateThread(NULL,0,producer,NULL,-1,&id);
    hThrds[1] = CreateThread(NULL,0,consumer,NULL,-1,&id);
    WaitForMultipleObjects(2,hThrds,true,-1);
    CloseHandle(hThrds[0]);
    CloseHandle(hThrds[1]);
    return 0;
}
看看这个程序哪有问题
2012-07-16 20:09
小谦2
Rank: 1
等 级:新手上路
帖 子:3
专家分:7
注 册:2012-7-16
收藏
得分:7 
你妹啊,这么长
2012-07-18 00:25
半年换半生
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2012-7-21
收藏
得分:7 
你妹啊
2012-07-21 15:40
快速回复:麻烦大家帮我看看消费者与生产者,我的程序哪出了问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029576 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved