zoukankan      html  css  js  c++  java
  • 用C++实现多个生产者 和多个消费者之间的同步

    #include<iostream>
    #include<math.h>
    #include<vector>
    #include<queue>
    #include"printBinaryTreeInLevel.h"
    #include<fstream>
    #include<map>
    #include<sstream>
    #include<stack>
    #include<Windows.h>
    using namespace std;
    int n=0;
    int turn;
    int instrest[2];
    void thread1()
    {
    /*while(true) // 软件方法实现临界区的代码
    {
    turn=0;
    instrest[0]=1;
    while(turn==0&&instrest[1]==1);
    cout<<"thread1:"<<n<<endl;
    n++;
    instrest[0]=0;
    }*/
    while(true)
    {
    turn=0;
    instrest[0]=1;
    while(turn==0&&instrest[1]==1);
    cout<<"thread1:"<<n<<endl;
    n++;
    instrest[0]=0;
    }
    }
    void thread2()
    {

    /*while(true) // 软件方法实现临界区的方法
    {
    turn=1;
    instrest[1]=1;
    while(turn==1&&instrest[0]==1);
    cout<<"thread2:"<<n<<endl;
    n++;
    instrest[1]=0;

    }*/
    while(true)
    {
    turn=1;
    instrest[1]=1;
    while(turn==1&&instrest[0]==1);
    cout<<"thread2:"<<n<<endl;
    n++;
    instrest[1]=0;

    }
    }
    const int N=10;
    int data[N]={0};
    int in=0;
    int out=0;
    HANDLE full,empty;
    HANDLE mutex;
    int produce_item()
    {
    return n++;
    }
    void produce(LPVOID lp) // 生产者模式
    {
    int id=(int)lp;
    while(true)
    {

    WaitForSingleObject(empty,INFINITE);
    WaitForSingleObject(mutex,INFINITE);

    int p=produce_item();
    cout<<id<<"生产:"<<n<<endl;
    data[in]=p;
    in=(in+1)%N;

    ReleaseSemaphore(mutex,1,NULL);
    ReleaseSemaphore(full,1,NULL);

    }
    }
    void consumer(LPVOID lp)// 消费者模式
    {
    int id=(int)lp;
    while(true)
    {
    WaitForSingleObject(full,INFINITE);
    WaitForSingleObject(mutex,INFINITE);
    cout<<id<<"消费:"<<data[out]<<endl;
    out=(out+1)%N;
    ReleaseSemaphore(mutex,1,NULL);
    ReleaseSemaphore(empty,1,NULL);
    }
    }
    void main()
    {
    empty = CreateSemaphore( // 创建信号量
    NULL, // default security attributes
    N, // initial count 当前值
    N, // maximum count 最大值
    NULL); // unnamed semaphore
    if (empty == NULL)
    {
    printf("CreateSemaphore error: %d ", GetLastError());
    return;
    }
    full = CreateSemaphore(
    NULL, // default security attributes
    0, // initial count
    N, // maximum count
    NULL); // unnamed semaphore
    if (full == NULL)
    {
    printf("CreateSemaphore error: %d ", GetLastError());
    return;
    }
    mutex = CreateSemaphore(
    NULL, // default security attributes
    1, // initial count
    1, // maximum count
    NULL); // unnamed semaphore
    if (full == NULL)
    {
    printf("CreateSemaphore error: %d ", GetLastError());
    return;
    }

    DWORD threadId;
    for(int i=1;i<=10;i++)// 生产者消费者各创建10个线程
    {
    HANDLE handle1=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)produce,(LPVOID)i,0,&threadId);
    cout<<"thread1 ID:"<<threadId<<endl;
    HANDLE handle2=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)consumer,(LPVOID)i,0,&threadId);
    cout<<"thread2 ID:"<<threadId<<endl;
    }

    system("pause");
    }

  • 相关阅读:
    sync 解释
    USB枚举详细过程剖析(转)
    内核早期内存分配器:memblock
    LTE:eMBMS架构
    对linux内核中jiffies+Hz表示一秒钟的理解
    android的USB MTP && USB CDC/USBnet(ECM, NCM, ACM) && USB gardget
    Install Shield中调用devcon自动安装硬件驱动程序
    利用 devcon.exe实现自动安装驱动(转)
    linux系统IO调度算法
    ZooKeeper原理详解及常用操作
  • 原文地址:https://www.cnblogs.com/dyc0113/p/3174042.html
Copyright © 2011-2022 走看看