我贴一下我的代码,为什么会出现返回87的问题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <windows.h>
#include <map>
using namespace std;
#define random (rand()*1000)/RAND_MAX // random audience
int movieNumber;
int Count[1000];
HANDLE Mutex[1000];
HANDLE HallMutex[3];
map<int ,DWORD >MAP;
DWORD WINAPI VideoShow(LPVOID vid) {
int i = (int)vid;
printf("来个一个观众,想看%d影片\n",i);
WaitForSingleObject(Mutex[i],-1);
Count[i]++;
if (Count[i] == 1) {
DWORD index = WaitForMultipleObjects(3,HallMutex,FALSE,-1);
if (index == WAIT_OBJECT_0 || index == WAIT_OBJECT_0 + 1 || index == WAIT_OBJECT_0 + 2) {
MAP[i] = index - WAIT_OBJECT_0;
printf("观众进入录像厅\n");
printf("录像厅%d正在放映影片%d,有%d个观众\n",index,i,1);
}
else {
ExitProcess(GetLastError());
}
}
else {
printf("观众进入录像厅\n");
printf("录像厅%d正在放映影片%d,有%d个观众\n",MAP[i],i,Count[i]);
}
ReleaseMutex(Mutex[i]);
Sleep(2000);
WaitForSingleObject(Mutex[i],-1);
Count[i]--;
printf("有一个观众从正在播放影片%d的录像厅%d离开,还有%d个人\n",i,MAP[i],Count[i]);
if (Count[i] == 0) {
printf("录像厅%d放映结束\n",MAP[i]);
ReleaseMutex(HallMutex[MAP[i]]);
}
ReleaseMutex(Mutex[i]);
}
int main () {
printf("请输入电影院里电影的数目\n");
cin >> movieNumber;
for (int i = 0;i < 3;i++) {
HallMutex[i] = CreateMutex(NULL,0,NULL);
}
for (int i = 0;i < movieNumber;i++) {
Mutex[i] = CreateMutex(NULL,0,NULL);
}
HANDLE hThread;
while (1) {
Sleep(random);
DWORD a = rand() % movieNumber;
hThread = CreateThread(NULL,0,VideoShow,LPVOID(a),0,NULL);
}
}