11
2+ // Copyright Sebastian Jeckel 2014.
3+ // Distributed under the Boost Software License, Version 1.0.
4+ // (See accompanying file LICENSE_1_0.txt or copy at
5+ // http://www.boost.org/LICENSE_1_0.txt)
26
7+ #include < iostream>
8+
9+ #include " tbb/tick_count.h"
10+
11+ #include " react/Domain.h"
12+ #include " react/Event.h"
13+ #include " react/Observer.h"
14+
15+ // /////////////////////////////////////////////////////////////////////////////////////////////////
16+ // / Example 1 - Asynchronous transactions
17+ // /////////////////////////////////////////////////////////////////////////////////////////////////
18+ namespace example1
19+ {
20+ using namespace react ;
21+ using namespace std ;
22+
23+ REACTIVE_DOMAIN (D, sequential_concurrent)
24+
25+ class Sensor
26+ {
27+ public:
28+ USING_REACTIVE_DOMAIN (D)
29+
30+ EventSourceT<int > Samples = MakeEventSource<D,int >();
31+ };
32+
33+ void Run ()
34+ {
35+ cout << " Example 1 - Asynchronous transactions" << endl;
36+
37+ Sensor mySensor;
38+
39+ Observe (mySensor.Samples , [] (int v) {
40+ cout << v << std::endl;
41+ });
42+
43+ TransactionStatus status;
44+
45+ AsyncTransaction<D>(status, [&] {
46+ mySensor.Samples << 30 << 31 << 31 << 32 ;
47+ });
48+
49+ AsyncTransaction<D>(status, [&] {
50+ mySensor.Samples << 40 << 41 << 51 << 62 ;
51+ });
52+
53+ // Waits until both transactions are completed.
54+ // This does not mean that both transactions are interleaved.
55+ status.Wait ();
56+
57+ cout << endl;
58+ }
59+ }
60+
61+ // /////////////////////////////////////////////////////////////////////////////////////////////////
62+ // / Example 2 - Transaction merging
63+ // /////////////////////////////////////////////////////////////////////////////////////////////////
64+ namespace example2
65+ {
66+ using namespace react ;
67+ using namespace std ;
68+
69+ REACTIVE_DOMAIN (D, sequential_concurrent)
70+
71+ class Sensor
72+ {
73+ public:
74+ USING_REACTIVE_DOMAIN (D)
75+
76+ EventSourceT<int > Samples = MakeEventSource<D,int >();
77+ };
78+
79+ const int K = 100000 ;
80+
81+ namespace v1
82+ {
83+ void Run ()
84+ {
85+ cout << " Example 2 - Transaction merging (no merging)" << endl;
86+
87+ Sensor mySensor;
88+ int sum = 0 ;
89+
90+ Observe (mySensor.Samples , [&] (int v) {
91+ sum += v;
92+ });
93+
94+ TransactionStatus status;
95+
96+ cout << " Executing " << K << " async transactions..." ;
97+
98+ auto t0 = tbb::tick_count::now ();
99+
100+ for (int i=0 ; i < K; i++)
101+ {
102+ AsyncTransaction<D>(status, [&] {
103+ mySensor.Samples << 3 << 4 << 2 << 1 ;
104+ });
105+ }
106+
107+ status.Wait ();
108+
109+ double d = (tbb::tick_count::now () - t0).seconds ();
110+
111+ cout << " done." << endl;
112+
113+ cout << " Sum: " << sum << endl;
114+ cout << " Time: " << d << endl;
115+
116+ cout << endl;
117+ }
118+ }
119+
120+ namespace v2
121+ {
122+ void Run ()
123+ {
124+ cout << " Example 2 - Transaction merging (allow merging)" << endl;
125+
126+ Sensor mySensor;
127+ int sum = 0 ;
128+
129+ Observe (mySensor.Samples , [&] (int v) {
130+ sum += v;
131+ });
132+
133+ TransactionStatus status;
134+
135+ cout << " Executing " << K << " async transactions..." ;
136+
137+ auto t0 = tbb::tick_count::now ();
138+
139+ for (int i=0 ; i < K; i++)
140+ {
141+ AsyncTransaction<D>(allow_merging, status, [&] {
142+ mySensor.Samples << 3 << 4 << 2 << 1 ;
143+ });
144+ }
145+
146+ status.Wait ();
147+
148+ double d = (tbb::tick_count::now () - t0).seconds ();
149+
150+ cout << " done." << endl;
151+
152+ cout << " Sum: " << sum << endl;
153+ cout << " Time: " << d << endl;
154+
155+ cout << endl;
156+ }
157+ }
158+ }
159+
160+ // /////////////////////////////////////////////////////////////////////////////////////////////////
161+ // / Run examples
162+ // /////////////////////////////////////////////////////////////////////////////////////////////////
3163int main ()
4164{
165+ example1::Run ();
166+ example2::v1::Run ();
167+ example2::v2::Run ();
168+
169+ return 0 ;
5170}
0 commit comments