-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaiter.cpp
More file actions
51 lines (48 loc) · 812 Bytes
/
waiter.cpp
File metadata and controls
51 lines (48 loc) · 812 Bytes
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
#include "waiter.h"
#include "fork.h"
#include "QMutex"
Waiter * Waiter::instance=NULL;
Waiter::Waiter()
{
TakenForks=0;
}
Waiter * Waiter::getWaiter()
{
if (instance!=NULL)
{
return instance;
}
else
{
instance= new Waiter;
return instance;
}
}
bool Waiter::AllowToTakeFork(Fork * i)
{
WaiterMutex.lock();
//i->MutexFork.lock();
if (i->Owner==-1 && TakenForks<4)
{
TakenForks++;
WaiterMutex.unlock();
//i->MutexFork.unlock();
return true;
}
else {
WaiterMutex.unlock();
//i->MutexFork.unlock();
return false;
}
}
void Waiter::ReturningofTheFork(Fork *i)
{
{
WaiterMutex.lock();
TakenForks--;
WaiterMutex.unlock();
//i->MutexFork.lock();
i->Owner=-1;
//i->MutexFork.unlock();
}
}