-
Notifications
You must be signed in to change notification settings - Fork 1
/
Banker.cpp
73 lines (58 loc) · 1.45 KB
/
Banker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <Windows.h>
#include <process.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include "Banker_Process.h"
#include "Banker_System.h"
#define MAX_THREAD 64
#define MAX_AVAILABLE 200000
typedef struct Param
{
System *system;
int uid;
}Param;
unsigned int __stdcall myThread(void *param)
{
unsigned int need = GetCurrentThreadId();
Param *pa = static_cast<Param*>(param);
srand(need);
do{
need = rand() % MAX_AVAILABLE;
}while(need == 0);
Process pro(need,0,pa->uid);
while(pro.getOwnNeed() < pro.getMaxNeed())
{
unsigned int resource = 0;
do{
if(pro.getMaxNeed() - pro.getOwnNeed() > 1)
resource = rand()% (pro.getMaxNeed() - pro.getOwnNeed());
else
resource = 1;
}while(resource == 0);
bool res = pro.requestResource(resource,*(pa->system));
}
std::string prompt;
prompt = prompt + "\n~~~~~~Thread\t" + std::to_string(pa->uid) + "(" + std::to_string(pro.getMaxNeed()) + ")" + " has request success! now free source\n";
std::cout << prompt;
pro.freeSource(*(pa->system));
return 0;
}
int main(int argc, char const *argv[])
{
HANDLE hThread[MAX_THREAD];
System s(MAX_AVAILABLE);
Param par[MAX_THREAD];
for(int i=0; i < MAX_THREAD; ++i)
{
par[i].system = &s;
par[i].uid = i;
hThread[i] = (HANDLE)_beginthreadex(NULL, 0, myThread, par + i, 0, NULL);
}
WaitForMultipleObjects(MAX_THREAD, hThread, TRUE, INFINITE);
for(int i = 0; i< MAX_THREAD; ++i)
{
CloseHandle(hThread[i]);
}
return 0;
}