现有部分代码如下:
// 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;
}
求指导