-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathComditionVariableSample-video2.cpp
More file actions
118 lines (112 loc) · 3.01 KB
/
ComditionVariableSample-video2.cpp
File metadata and controls
118 lines (112 loc) · 3.01 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <thread>
#include <iostream>
#include <queue>
#include <functional>
using namespace std;
template <typename T>
class MutexSafe
{
private:
mutex _mutex;
T* _resource;
T* operator ->(){}
T& operator &(){}
public:
MutexSafe(T* resource):_resource(resource){}
~MutexSafe(){delete _resource;}
void lock()
{
_mutex.lock();
}
void unlock()
{
_mutex.unlock();
}
bool try_lock()
{
return _mutex.try_lock();
}
mutex& Mutex()
{
return _mutex;
}
T& Acquire (unique_lock<MutexSafe<T>>& lock)
{
MutexSafe<T> *_safe = lock.mutex();
if(&_safe->Mutex()!=&_mutex)
{
throw "wrong lock object passed to Acquire function.\n";
}
return *_resource;
}
T& Acquire (unique_lock<mutex>& lock)
{
if(lock.mutex()!=&_mutex)
{
throw "wrong lock object passed to Acquire function.\n";
}
return *_resource;
}
};
struct StockBlackboard
{
float price;
const string name;
StockBlackboard(const string stockName, float stockPrice=0):name(stockName),price(stockPrice){}
};
typedef MutexSafe<StockBlackboard> StockSafe;
void PeterUpdateStock_Notify (StockSafe & safe, condition_variable& condition)
{
for (int i=0;i<4;i++)
{
{
unique_lock<StockSafe> lock(safe);
StockBlackboard& stock= safe.Acquire(lock);
if(i==2)
stock.price=99;
else
stock.price = abs(rand()% 100);
cout<<"Peter udpated the price to $"<<stock.price<<endl;
if(stock.price>90)
{
lock.unlock();
cout<<"Peter notified Danny at price $"<<stock.price<<", ";
condition.notify_one();
this_thread::sleep_for(std::chrono::milliseconds(10));
lock.lock();
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(rand()%10));
}
}
void DannyWait_ReadStock(StockSafe & safe, condition_variable& priceCondition)
{
unique_lock<mutex> lock(safe.Mutex());
cout<<"Danny is waiting for the right price to sell..."<<endl;
priceCondition.wait(lock);
StockBlackboard& stock= safe.Acquire(lock);
if(stock.price>90)
cout<<"Danny sell at: $"<<stock.price<<endl;
else
{
cout<<"False alarm at $"<<stock.price<<" wait again..."<<endl;
priceCondition.wait(lock);
StockBlackboard& stock= safe.Acquire(lock);
if(stock.price>90)
cout<<"Danny sell at: $"<<stock.price<<endl;
}
}
void TestConditionVariable()
{
StockSafe safe (new StockBlackboard("APPL",30));
condition_variable priceCondition;
thread DannyThread(DannyWait_ReadStock, std::ref(safe), std::ref(priceCondition));
thread PeterThread( PeterUpdateStock_Notify, std::ref(safe), std::ref(priceCondition));
PeterThread.join();
DannyThread.join();
}
int main()
{
TestConditionVariable();
return 0;
}