-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.cpp
94 lines (80 loc) · 2.13 KB
/
sample.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
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
#include <mutex>
#include <string>
#include <memory>
#include <vector>
#include <iostream>
#include "lockstrap_simple.h"
class Simple {
int a;
float b;
std::string c;
std::vector<long> x;
LCKSTRAPSIMP(Simple, std::mutex, a,b,c,x);
};
void TestSimple()
{
Simple simp;
auto al = simp.access();
// At this point, 'simp' data is locked and we can access it. It
// will be unlocked when 'al' gets out of scope.
al.a() = 2;
al.b() = 2.1;
al.c() = "2.X";
al.x().emplace_back(2L);
std::cout << al.a() << " " << al.b() << " " << al.c() << " " << al.x().size() << std::endl;
al.a() = 3;
al.b() = 3.1;
al.c() = "3.X";
al.x().emplace_back(3L);
// With C++14 compiler, you can replace 'Simple::locker' with 'auto'
simp.with([](Simple::locker &&l) {
// Within the "with block" the locker holds the lock, so it will be
// unlocked at the end of this lambda.
std::cout << l.a() << " " << l.b() << " " << l.c() << " " << l.x().size() << std::endl;
l.a() = 4;
l.b() = 4.1;
l.c() = "4.X";
l.x().emplace_back(4L);
});
std::cout << al.a() << " " << al.b() << " " << al.c() << " " << al.x().size() << std::endl;
}
#include "lockstrap.h"
class User {
int a;
float b;
std::string c;
std::vector<long> x;
LOCKSTRAP(User, std::mutex, a,b,c,x);
};
void TestRegular()
{
User usr;
// Except for a more "natural" syntax, that is, avoiding "()", the
// usage is the same for the "regular" as for the "simple"
// lock-strap.
auto al = usr.access();
al.a = 2;
al.b = 2.2;
al.c = "2.Y";
al.x.emplace_back(2L);
std::cout << al.a << " " << al.b << " " << al.c << " " << al.x.size() << std::endl;
al.a = 3;
al.b = 3.2;
al.c = "3.Y";
al.x.emplace_back(3L);
// With C++14 compiler, you can replace 'User::locker' with 'auto'
usr.with([](User::locker &&l) {
std::cout << l.a << " " << l.b << " " << l.c << " " << l.x.size() << std::endl;
l.a = 4;
l.b = 4.2;
l.c = "4.Y";
l.x.emplace_back(4L);
});
std::cout << al.a << " " << al.b << " " << al.c << " " << al.x.size() <<std::endl;
}
int main()
{
TestSimple();
TestRegular();
return 0;
}