-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththread_safety.cpp
More file actions
83 lines (72 loc) · 1.85 KB
/
thread_safety.cpp
File metadata and controls
83 lines (72 loc) · 1.85 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
#include "observer.h"
#include "safer_observable.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**
* Observable: Person notifies when the age field is changed.
*/
class Person : public SaferObservable<Person> {
int age{0};
public:
Person() {}
explicit Person(int age) : age(age) {}
int get_age() const { return age; }
void set_age(int age) {
if (this->age == age)
return;
this->age = age;
notify(*this, "age");
}
};
/**
* Observer: Console observes age changes and prints them.
*/
struct ConsolePersonObserver : public Observer<Person> {
private:
// implements field_changed() callback
void field_changed(Person &source, const std::string &field_name) override {
cout << "[ConsolePersonObserver] Person's " << field_name
<< " has changed to ";
if (field_name == "age")
cout << source.get_age();
cout << ".\n";
}
};
/**
* Observer: Traffic buro observes age changes.
*/
struct TrafficAdministration : Observer<Person> {
// implements field_changed() callback
void field_changed(Person &source, const std::string &field_name) override {
if (field_name == "age") {
if (source.get_age() < 17)
cout << "[TrafficAdministration] Whoa there, you're not old enough to "
"drive!\n";
else {
// unsubscribe happens inside the notification -> possible deadlock!
cout << "[TrafficAdministration] Oh, ok, we no longer care!\n";
source.unsubscribe(*this);
}
}
}
};
int main() {
Person p;
// multiple observers
// ConsolePersonObserver cpo;
TrafficAdministration ta;
p.subscribe(ta);
// p.subscribe(cpo);
try {
p.set_age(15);
p.set_age(16);
p.set_age(17);
p.set_age(18);
p.set_age(19);
} catch (const std::exception &e) {
cout << "Oops, " << e.what() << "\n";
}
return 0;
}