又5 2022-12-16 16:57 采纳率: 81.8%
浏览 54
已结题

请问该如何写这个并发程序代码

现有部分代码如下:

// multithread.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <process.h>
#include <windows.h>
using namespace std;
#define N 10  // 缓冲区数目
//函数声明
DWORD WINAPI fun1( LPVOID lpParam );
DWORD WINAPI fun2( LPVOID lpParam );
DWORD WINAPI fun3( LPVOID lpParam );
DWORD WINAPI Thread_producer( LPVOID lpParam ); 
DWORD WINAPI Thread_conducter( LPVOID lpParam ); 
void print_array();
//定义全局变量
bool isStop = false;
int data[N];
HANDLE empty,full,mutex;    
int in=0,out=0;
void  main(void)
{
    DWORD dwThreadId1,dwThreadId2,dwThreadId3,dwThreadId4,dwThreadId5,
        dwThreadId6,dwThreadId7;
    HANDLE hThread1,hThread2,hThread3,hThread4,hThread5,hThread6,hThread7;
    int x;
    while (true) {
        printf("请输入要选择的操作:\n0:退出\n1:打印data数组中的元素\n2:依次调用两个函数\n3:启动两个一样的线程\n4:资源共享的两个互斥型线程同步控制\n5:生产者-消费者线程同步控制\n6:停止线程\n");
        scanf("%d",&x);
        switch (x) {
        case 0:
            exit(0);
        case 1:
            print_array();
            break;
        case 2:
            fun1((void *)11);
            fun1((void *)22);
            break;
        case 3:
            //创建两个相同的子线程,启动并观察,一个传入整数11,一个传入22
            hThread1 = CreateThread(NULL,0,fun1, (LPVOID)11, 0 ,&dwThreadId1);                
            hThread2 = CreateThread(NULL,0,fun1, (LPVOID)22, 0 ,&dwThreadId2);
            break;
        case 4:
            fun2((void *)200);
            fun3((void *)300);
            //创建两个不同的子线程,启动并观察同步控制过程
            //HANDLE hThread3,hThread4;
            //DWORD dwThreadId3,dwThreadId4;
            HANDLE hSem; //创建信号量hSem
            hSem = CreateSemaphore(NULL,1,1,NULL); //创建一个信号量,初值为1,最大为1
            //创建两个子线程,信号量句柄hSem作为线程参数传递给线程
            void P(hSem)
            {
                hSem = hSem - 1;
            }
            void V(hSem)
            {
                hSem = hSem + 1;
            }
            hThread3 = CreateThread(NULL,0,fun2, (LPVOID)hSem,0, &dwThreadId3);
            hSem = hSem + 1;
            hThread4 = CreateThread(NULL,0,fun3, (LPVOID)hSem,0, &dwThreadId4); 
            break;
        case 5:
            //生产者-消费者问题
             //添加代码:创建生产者私有信号量empty,标志空缓冲区数量,初值为10,最大为10
             //添加代码:创建消费者私有信号量full,标志有产品的缓冲区数量,初值为0,最大为10
             //添加代码:创建对临界区(缓冲池)互斥访问的信号量mutex,初值为1,最大为1
        


            //创建2个生产者线程
            hThread5 = CreateThread(NULL,0,Thread_producer,0,0,&dwThreadId5);                               
            hThread6 = CreateThread(NULL,0,Thread_producer,0,0,&dwThreadId6); 
            
            // 添加代码:创建1个消费者线程 hThread7 
        
            break;
        case 6:
            isStop = true;
            break;
        default:
            printf( "选项不正确\n");
        }
    }
}
DWORD WINAPI fun1( LPVOID lpParam ) 
{
    int count,i;
    count = (int)lpParam;
    for (i=0;i<10;i++)
    { 
        data[i]=count;
        printf("data[%d]=%d\n",i+1,data[i]);
        Sleep(rand()%200); //产生0~200的随机数,随机延迟线程执行
    }
    return 0; 
} 
DWORD WINAPI fun2( LPVOID lpParam ) 
{
    int count = 0,i;
    HANDLE hSem = (HANDLE)lpParam;
    while(!isStop)
    {
        Sleep(1000);
    
        //添加代码:对信号量hSem做PV操作,实现线程互斥

        printf("thread2 wake\n ");
        for (i=0;i<10;i++)
        { 
            data[i]=200;
            printf("data[%d]=%d\n",i+1,data[i]);
            Sleep(100);
        }
        printf("now,thread2 is waiting.\n");

        //添加代码:对信号量hSem做PV操作,实现线程互斥

    }
    return 0; 
} 

DWORD WINAPI fun3( LPVOID lpParam ) 
{
    int count = 0,i;
    HANDLE hSem = (HANDLE)lpParam;
    while(!isStop)
    {
        //添加代码:对信号量hSem做PV操作,实现线程互斥

        printf("thread3 wake\n ");
        for (i=0;i<10;i++)
        { 
            data[i]=300;
            printf("data[%d]=%d\n",i+1,data[i]);
            Sleep(100);
        }
        printf("now,thread3 is waiting.\n");

        //添加代码:对信号量hSem做PV操作,实现线程互斥

    }
    return 0; 
} 

void print_array()
{//依次打印数组元素
    int i;
    printf("\nthe data array is:\n");
    for(i=0;i<10;i++)
        printf("data[%d]=%d\n",i,data[i]);
    
}

DWORD WINAPI Thread_producer( LPVOID lpParam ) 
{ //生产者进程
    while(!isStop)
    { 
        Sleep(10);

       //添加代码:对信号量mutex,full,empty做PV操作,实现消费者进程和生产者进程的同步     
        
        data[in]=rand();
        printf("生产者向%d号缓冲区放入了一个数据%d: \t", in, data[in]);
        print_array();
        in=(in+1)%N;
        
        
       //添加代码:对信号量mutex,full,empty做PV操作,实现消费者进程和生产者进程的同步     


    }
    return 0; 
} 

DWORD WINAPI Thread_conducter( LPVOID lpParam ) 
{ //消费者进程
    while(!isStop)
    { 
        Sleep(10);


        //添加代码:(1)对信号量mutex,full,empty做PV操作,实现消费者进程和生产者进程的同步 
        //          (2)从缓冲区取出一个元素,并打印取出的元素信息,打印缓冲区信息
        //          (3)对信号量mutex,full,empty做PV操作,实现消费者进程和生产者进程的同步 


        
    }
    return 0; 
}


求指导

  • 写回答

4条回答 默认 最新

  • |__WhoAmI__| 2022-12-16 17:58
    关注
    // multithread.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    #include <iostream>
    #include <process.h>
    #include <windows.h>
    using namespace std;
    #define N 10  // 缓冲区数目
    
    // 函数声明
    DWORD WINAPI fun1(LPVOID lpParam);
    DWORD WINAPI fun2(LPVOID lpParam);
    DWORD WINAPI fun3(LPVOID lpParam);
    DWORD WINAPI Thread_producer(LPVOID lpParam); 
    DWORD WINAPI Thread_conducter(LPVOID lpParam); 
    void print_array();
    
    // 定义全局变量
    bool isStop = false;
    int data[N];
    HANDLE empty, full, mutex;    
    int in = 0, out = 0;
    
    int main(void)
    {
        DWORD dwThreadId1, dwThreadId2, dwThreadId3, dwThreadId4, dwThreadId5,
            dwThreadId6, dwThreadId7;
        HANDLE hThread1, hThread2, hThread3, hThread4, hThread5, hThread6, hThread7;
        int x;
        while (true) {
            printf("请输入要选择的操作:\n0:退出\n1:打印data数组中的元素\n2:依次调用两个函数\n3:启动两个一样的线程\n4:资源共享的两个互斥型线程同步控制\n5:生产者-消费者线程同步控制\n6:停止线程\n");
            scanf("%d", &x);
            switch (x) {
            case 0:
                exit(0);
            case 1:
                print_array();
                break;
            case 2:
                fun1((void *)11);
                fun1((void *)22);
                break;
            case 3:
                // 创建两个相同的子线程,启动并观察,一个传入整数11,一个传入22
                hThread1 = CreateThread(NULL, 0, fun1, (LPVOID)11, 0 , &dwThreadId1);                
                hThread2 = CreateThread(NULL, 0, fun1, (LPVOID)22, 0 , &dwThreadId2);
                break;
    
    case 4:
    // 创建两个不同的子线程,启动并观察同步控制过程
    HANDLE hSem; // 创建信号量hSem
    hSem = CreateSemaphore(NULL, 1, 1, NULL); // 创建一个信号量,初值为1,最大为1
    // 创建两个子线程, 信号量句柄hSem作为线程参数传递给线程
    hThread3 = CreateThread(NULL, 0, fun2, (LPVOID)hSem, 0, &dwThreadId3);
    hThread4 = CreateThread(NULL, 0, fun3, (LPVOID)hSem, 0, &dwThreadId4);
    break;
    case 5:
    // 创建信号量empty和full,并初始化为N和0
    empty = CreateSemaphore(NULL, N, N, NULL);
    full = CreateSemaphore(NULL, 0, N, NULL);
    // 创建互斥量mutex
    mutex = CreateMutex(NULL, FALSE, NULL);
    // 创建生产者线程和消费者线程
    hThread5 = CreateThread(NULL, 0, Thread_producer, NULL, 0, &dwThreadId5);
    hThread6 = CreateThread(NULL, 0, Thread_conducter, NULL, 0, &dwThreadId6);
    break;
    case 6:
    // 停止线程
    isStop = true;
    break;
    }
    }
    return 0;
    }
    
    // 函数定义
    
    DWORD WINAPI fun1(LPVOID lpParam)
    {
    int x = (int)lpParam;
    printf("fun1: x = %d\n", x);
    return 0;
    }
    
    DWORD WINAPI fun2(LPVOID lpParam)
    {
    HANDLE hSem;
    hSem = (HANDLE)lpParam;
    WaitForSingleObject(hSem, INFINITE);
    printf("fun2: 获得信号量,正在运行...\n");
    Sleep(1000);
    ReleaseSemaphore(hSem, 1, NULL);
    return 0;
    }
    
    DWORD WINAPI fun3(LPVOID lpParam)
    {
    HANDLE hSem;
    hSem = (HANDLE)lpParam;
    WaitForSingleObject(hSem, INFINITE);
    printf("fun3: 获得信号量,正在
    
    运行...\n");
    Sleep(1000);
    ReleaseSemaphore(hSem, 1, NULL);
    return 0;
    }
    
    DWORD WINAPI Thread_producer(LPVOID lpParam)
    {
    while (!isStop) {
    WaitForSingleObject(empty, INFINITE);
    WaitForSingleObject(mutex, INFINITE);
    // 生产者生产一个产品
    data[in] = rand();
    printf("生产者: data[%d] = %d\n", in, data[in]);
    in = (in + 1) % N;
    ReleaseMutex(mutex);
    ReleaseSemaphore(full, 1, NULL);
    Sleep(500);
    }
    return 0;
    }
    
    DWORD WINAPI Thread_conducter(LPVOID lpParam)
    {
    while (!isStop) {
    WaitForSingleObject(full, INFINITE);
    WaitForSingleObject(mutex, INFINITE);
    // 消费者消费一个产品
    printf("消费者: data[%d] = %d\n", out, data[out]);
    out = (out + 1) % N;
    ReleaseMutex(mutex);
    ReleaseSemaphore(empty, 1, NULL);
    Sleep(1000);
    }
    return 0;
    }
    
    void print_array()
    {
    printf("打印数组中的元素: ");
    for (int i = 0; i < N; i++) {
    printf("%d ", data[i]);
    }
    printf("\n");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月18日
  • 已采纳回答 12月16日
  • 创建了问题 12月16日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表