zoukankan      html  css  js  c++  java
  • c实现生产者消费者问题。 windows下。

    #include <stdio.h>
    #include <windows.h>

    #define P(S) WaitForSingleObject(S,INFINITE)//定义Windows下的P操作
    #define V(S) ReleaseSemaphore(S,1,NULL)//定义Windows下的V操作

    //生产者 消费者 缓冲区
    #define num_of_productors 2
    #define num_of_consumers 3
    #define num_of_buffers 3

    int nextp = 1; //下一个产品的编号
    int in = 0, out = 0; //分别指向下一个存放产品和取出产品的缓冲区编号
    int g_Buffer[num_of_buffers] = { 0 }; //缓冲区初始值为空

    HANDLE g_hSemaphoreEmpty, g_hSemaphoreFull, g_hMutex;

    //生产者线程函数
    DWORD WINAPI ProducerThreadFun(LPVOID pM)
    {
    Sleep(100);
    do{
    int id = *((int*)(pM)); //生产者编号
    P(g_hSemaphoreEmpty);
    P(g_hMutex);
    g_Buffer[in] = nextp;
    printf("生产者%d将数据%d放入缓冲区%d ", id, nextp, in);
    nextp++;
    in = (in + 1) % num_of_buffers;
    V(g_hMutex);
    V(g_hSemaphoreFull);
    Sleep(100);
    if (nextp > 12) break;
    } while(true);
    return 0;
    }
    //消费者线程函数
    DWORD WINAPI ConsumerThreadFun(LPVOID pM)
    {
    Sleep(100);
    do
    {
    int id = *((int*)(pM)); //消费者编号
    P(g_hSemaphoreFull);
    P(g_hMutex);
    //nextp = g_Buffer[out];
    printf(" 消费者%d从缓冲区%d中取数据%d ", id, out, g_Buffer[out]);
    out = (out + 1) % num_of_buffers;
    V(g_hMutex);
    V(g_hSemaphoreEmpty);
    Sleep(100);
    } while (true);
    }
    int main()
    {
    int num[num_of_productors + num_of_consumers];
    printf(" 生产者消费者问题:%d生产者 %d消费者 %d缓冲区 ", num_of_productors, num_of_consumers, num_of_buffers);
    //初始值为num_of_buffers的信号量
    g_hSemaphoreEmpty = CreateSemaphore(NULL, num_of_buffers, num_of_buffers, NULL); //继承/初始化信号计数/最大信号计数/名称
    //初始值为0的信号量
    g_hSemaphoreFull = CreateSemaphore(NULL, 0, num_of_buffers, NULL);
    //互斥量
    g_hMutex = CreateSemaphore(NULL, 1, 1, NULL);

    const int THREADNUM = num_of_productors + num_of_consumers; //线程数
    HANDLE hThread[THREADNUM];

    for (int i = 0; i < num_of_productors; i++) //创建生产者线程
    {
    num[i] = i + 1;
    hThread[i] = CreateThread(NULL, 0, ProducerThreadFun, (LPVOID *)&num[i], 0, NULL);
    } //继承/初始栈大小/线程函数/向线程函数传递的参数/线程标志 0 表示创建后立即激活/保存新线程的ID
    for (int i = num_of_productors; i < num_of_productors + num_of_consumers; i++) //创建消费者者线程
    {
    num[i] = i - num_of_productors + 1;
    hThread[i] = CreateThread(NULL, 0, ConsumerThreadFun, (LPVOID *)&num[i], 0, NULL);
    }

    WaitForMultipleObjects(THREADNUM, hThread, TRUE, INFINITE);

    system("pause");
    return 0;
    }

  • 相关阅读:
    dnsServer SmartDNS / smartdns / DNS UDP 53
    springBoot 自定义注解 + 自定义异常捕获实战
    查询出ES库所有数据
    springBoot2.X 支持http、https访问
    配置ES IK分词器自定义字典
    搭建angular开发环境 运行 ng-alain框架
    【jQuery】 选择器
    【jQuery】 js 对象
    【C#】 URL Protocol
    【C#】 反射
  • 原文地址:https://www.cnblogs.com/ruoh3kou/p/8626284.html
Copyright © 2011-2022 走看看