-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakeprocess.h
53 lines (44 loc) · 1.01 KB
/
fakeprocess.h
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
#ifndef FAKE_PROCESS_H
#define FAKE_PROCESS_H
#include <iostream>
#include <stdlib.h>
#include <mpi.h>
class FakeProcess {
public:
FakeProcess(int rank, bool token, double rate = 0.5f):
_rank{rank},
_token{token},
_rate{rate}
{
srand(time(0));
}
bool useResource() {
if (_token) {
std::cout << "Process " << _rank << " used the resource." << std::endl;
}
return true;
}
bool doOtherStuff() {
std::cout << "Process " << _rank << " doing other stuff " << std::endl;
return false;
}
bool update() {
double chance = (double)rand()/(double)(RAND_MAX);
if (chance < _rate) { // Wants to access resource
return useResource();
} else { // is doing other stuff
return doOtherStuff();
}
}
bool token() {
return _token;
}
void token(bool t) {
_token = t;
}
private:
int _rank;
bool _token;
double _rate;
};
#endif