生产者消费者问题

#include <windows.h>
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;

const int nPool = 5;
const int nProducer = 3;
const int nConsumer = 2;
int nProductId = 0;
bool bRunning = true;
// 线程互斥
HANDLE hMutex;  
HANDLE hFull;
HANDLE hEmpty;

vector<int> vec;

DWORD WINAPI Product(LPVOID);
DWORD WINAPI Consume(LPVOID);

int main()
{
        hMutex = CreateMutex(NULL, FALSE,NULL);
        hFull = CreateSemaphore(NULL, nPool, nPool, NULL);
        hEmpty = CreateSemaphore(NULL, 0, nPool, NULL);

        HANDLE hProducter[nProducer];
        for (int i = 0; i < nProducer; ++i)
                 hProducter[i] = CreateThread(NULL, 0, Product, NULL, 0, NULL);
        HANDLE hConsumer[nConsumer];
        for (int i = 0; i < nConsumer; ++i)
                 hConsumer[i] = CreateThread(NULL, 0, Consume, NULL, 0, NULL);

         while (bRunning)
         {
                  if (getchar())
                          bRunning = false;
          }

          vec.clear();

          for (int i = 0; i < nProducer; ++i)
          {
                 if (NULL != hProducter[i])
                {
                        CloseHandle(hProducter[i]);
                        hProducter[i] = NULL;
                 }
          }

         for (int i = 0; i < nConsumer; ++i)
         {
                   if (NULL != hConsumer[i])
                   {
                           CloseHandle(hConsumer[i]);
                           hConsumer[i] = NULL;
                    }
          }

        if (NULL != hMutex)
        {
                CloseHandle(hMutex);
                hMutex = NULL;
         }

        if (NULL != hFull)
        {
                     CloseHandle(hFull);
                     hFull = NULL;
          }

        if (NULL != hEmpty)
        {
                CloseHandle(hEmpty);
                hEmpty = NULL;
         }
 
         return 0;
}

DWORD WINAPI Product(LPVOID para)
{
          while (bRunning)
          {
                WaitForSingleObject(hFull, INFINITE);
                WaitForSingleObject(hMutex, INFINITE);
                ++nProductId;
                nProductId %= nPool;
                vec.push_back(nProductId);
                cout << "Product " << nProductId << " ,  the Product Number in  Pool is  " << vec.size() << endl;
 
                Sleep(2000);
                ReleaseMutex(hMutex);
                ReleaseSemaphore(hEmpty, 1, NULL);
           }

           return 0;
}

DWORD WINAPI Consume(LPVOID para)
{
          while (bRunning)
          {
                  WaitForSingleObject(hEmpty, INFINITE);
                  WaitForSingleObject(hMutex, INFINITE);
                  vec.pop_back();
                  cout << "Consuem a product, and the Product Number in Pool is " << vec.size() << endl;
                  Sleep(1000);
                  ReleaseMutex(hMutex);
                  ReleaseSemaphore(hFull, 1, NULL);
           }

          return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值