-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathBasicSynchronization.cpp
More file actions
177 lines (131 loc) · 4.29 KB
/
BasicSynchronization.cpp
File metadata and controls
177 lines (131 loc) · 4.29 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright Sebastian Jeckel 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include "tbb/tick_count.h"
#include "react/state.h"
#include "react/event.h"
#include "react/observer.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 1 - Asynchronous transactions
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example1
{
using namespace react;
using namespace std;
Group group;
class Sensor
{
public:
EventSource<int> samples = EventSource<int>::Create(group);
};
void Run()
{
cout << "Example 1 - Asynchronous transactions" << endl;
Sensor mySensor;
auto obs = Observer::Create([&] (const auto& events)
{
for (auto t : events)
cout << t << endl;
}, mySensor.samples);
SyncPoint sp;
group.EnqueueTransaction([&]
{
mySensor.samples << 30 << 31 << 31 << 32;
}, sp);
group.EnqueueTransaction([&]
{
mySensor.samples << 40 << 41 << 51 << 62;
}, sp);
// Waits until both transactions are completed.
// This does not mean that both transactions are interleaved.
sp.Wait();
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 2 - Transaction merging
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example2
{
using namespace react;
using namespace std;
Group group;
class Sensor
{
public:
EventSource<int> samples = EventSource<int>::Create(group);
};
const int K = 10000;
namespace v1
{
void Run()
{
cout << "Example 2 - Transaction merging (no merging)" << endl;
Sensor mySensor;
int sum = 0;
auto obs = Observer::Create([&] (const auto& events)
{
for (int e : events)
sum += e;
}, mySensor.samples);
SyncPoint sp;
cout << "Executing " << K << " async transactions...";
auto t0 = tbb::tick_count::now();
for (int i=0; i < K; i++)
{
group.EnqueueTransaction([&]
{
mySensor.samples << 3 << 4 << 2 << 1;
}, sp);
}
sp.Wait();
double d = (tbb::tick_count::now() - t0).seconds();
cout << " done." << endl;
cout << " Sum: " << sum << endl;
cout << " Time: " << d << endl;
cout << endl;
}
}
namespace v2
{
void Run()
{
cout << "Example 2 - Transaction merging (allow merging)" << endl;
Sensor mySensor;
int sum = 0;
auto obs = Observer::Create([&] (const auto& events)
{
for (int e : events)
sum += e;
}, mySensor.samples);
SyncPoint sp;
cout << "Executing " << K << " async transactions...";
auto t0 = tbb::tick_count::now();
for (int i=0; i < K; i++)
{
group.EnqueueTransaction([&]
{
mySensor.samples << 3 << 4 << 2 << 1;
}, sp, TransactionFlags::allow_merging);
}
sp.Wait();
double d = (tbb::tick_count::now() - t0).seconds();
cout << " done." << endl;
cout << " Sum: " << sum << endl;
cout << " Time: " << d << endl;
cout << endl;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::Run();
example2::v1::Run();
example2::v2::Run();
return 0;
}