| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 479 人关注过本帖
标题:简单的生产者消费者问题,多线程达不到理想效果
只看楼主 加入收藏
bfas2010
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-12-24
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
简单的生产者消费者问题,多线程达不到理想效果
程序代码:
// BoundedBuffer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "process.h"
#define   MAXLEN  10

int  BUFFER = 0;

HANDLE hMutex;
HANDLE hSemaphore;
CRITICAL_SECTION cs;

void MyMutex();
void MyCriticalSection();
void MyThread();

int _tmain(int argc, _TCHAR* argv[])
{
//    MyMutex();
//    MyCriticalSection();
    MyThread();
    system("pause");
    return 0;
}

// 下面两线程用互斥量实现
DWORD WINAPI producer(LPVOID *a)
{
    while(1)
    {
        WaitForSingleObject(hMutex,INFINITE);
        if(BUFFER >=10)
        {
            printf("篮子已满****\n");
            Sleep(1000);
        }
        else
        {
            BUFFER++;
            printf("生产一个产品\n");
            Sleep(1000);
        }
        printf("此时的产品为: %d\n",BUFFER);
        Sleep(1000);
        ReleaseMutex(hMutex);
    }
}

DWORD WINAPI consumer(LPVOID *a)
{
    while(1)
    {
        WaitForSingleObject(hMutex,INFINITE);
        if(BUFFER <=0)
        {
            printf("篮子已空&&&&&\n");
            Sleep(200);
        }
        else
        {
            BUFFER--;
            printf("消费一个产品\n");
            Sleep(200);

        }
        printf("此时的产品为: %d\n",BUFFER);
        Sleep(1000);
        ReleaseMutex(hMutex);
    }
}

//下面两线程用临界区实现
DWORD WINAPI producer1(LPVOID *a)
{
    while(1)
    {
        EnterCriticalSection(&cs);
        if(BUFFER >=10)
        {
            printf("篮子已满****\n");
            Sleep(1000);
        }
        else
        {
            BUFFER++;
            printf("生产一个产品\n");
            Sleep(1000);
        }
        printf("此时的产品为: %d\n",BUFFER);
        Sleep(1000);
        LeaveCriticalSection(&cs);
    }
}

DWORD WINAPI consumer1(LPVOID *a)
{
    while(1)
    {
        EnterCriticalSection(&cs);
        if(BUFFER <=0)
        {
            printf("篮子已空&&&&&\n");
            Sleep(1000);
        }
        else
        {
            BUFFER--;
            printf("消费一个产品\n");
            Sleep(1000);
        }
        printf("此时的产品为: %d\n",BUFFER);
        Sleep(1000);
        LeaveCriticalSection(&cs);
    }
}

//下面两线程用信号量实现
DWORD WINAPI producer2(LPVOID *a)
{
    while(1)
    {
       WaitForSingleObject(hSemaphore,INFINITE);
        if(BUFFER >=10)
        {
            printf("篮子已满****\n");
            Sleep(1000);
        }
        else
        {
            BUFFER++;
            printf("生产一个产品\n");
            Sleep(1000);
        }
        printf("此时的产品为: %d\n",BUFFER);
        Sleep(1000);
        ReleaseSemaphore(hSemaphore,1,NULL);
    }
        return 23;
}

DWORD WINAPI consumer2(LPVOID *a)
{
    while(1)
    {
        WaitForSingleObject(hSemaphore,INFINITE);
        if(BUFFER <=0)
        {
            printf("篮子已空&&&&&\n");
            Sleep(1000);
        }
        else
        {
            BUFFER--;
            printf("消费一个产品\n");
            Sleep(1000);

        }
        printf("此时的产品为: %d\n",BUFFER);
        Sleep(1000);
        ReleaseSemaphore(hSemaphore,1,NULL);
    }
        return 22;
}

//互斥量
void MyMutex()
{
    hMutex = CreateMutex(NULL,FALSE,NULL);
    HANDLE handle1,handle2;
    DWORD dw1,dw2;
    handle1 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)producer,NULL,0,&dw1);
    handle2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)consumer,&dw1,0,&dw2);
    while(1);
}

//临界区
void MyCriticalSection()
{
    InitializeCriticalSection(&cs);
    HANDLE handle3,handle4;
    DWORD dw3,dw4;
    handle3 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)producer1,NULL,0,&dw3);
    handle4 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)consumer1,NULL,0,&dw4);
    while(1);
}

// 信号量
void MyThread()
{
    hSemaphore = CreateSemaphore(NULL,1,3,NULL);
    HANDLE handle5,handle6;
    DWORD dw5,dw6;
    handle5 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)producer2,NULL,0,&dw5);
    Sleep(1000);
    handle6 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)consumer2,NULL,0,&dw6);
    Sleep(1000);
    while(1);
}
MyMutex() 实现效果  生产,消费,生产,消费,生产,消费……
MyCriticalSection() 实现效果 生产,生产,生产->满,消费,消费,消费 -> 空
MyThread() 实现效果  与MyMutex() 一样   生产,消费,生产,消费……


问:为什么不是随机的生产消费,如生产,生产,消费,…………
                                消费,生产,生产,生产,消费……

能不能详细解释下上面3个实现方式为什么是有规律的?以及如何改正
搜索更多相关主题的帖子: 消费者 多线程 生产者 
2013-08-01 16:58
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:14 
膜拜

DO IT YOURSELF !
2013-08-02 09:52
快速回复:简单的生产者消费者问题,多线程达不到理想效果
数据加载中...
 
   



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

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