-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_resources.cpp
36 lines (36 loc) · 958 Bytes
/
shared_resources.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
#include <iostream>
using namespace std;
class SharedResource
{
private:
static int resource;
public:
void accessResource()
{
if (resource > 0) {
cout << "Access granted to the shared resource." << endl;
resource--;
} else {
cout << "Access denied Shared resource is currently unavailable." << endl;
}
}
static void setResourceCount(int count)
{
resource = count;
}
};
int SharedResource::resource = 0;
int main()
{
SharedResource obj1, obj2;
SharedResource::setResourceCount(5);
obj1.accessResource(); // Access granted
obj2.accessResource(); // Access granted
obj1.accessResource(); // Access granted
obj2.accessResource(); // Access granted
obj1.accessResource(); // Access granted
obj2.accessResource(); // Access granted
obj1.accessResource(); // Access denied
obj2.accessResource(); // Access denied
return 0;
}