Skip to content

Commit 2c77bd7

Browse files
committed
Examples cleanup.
1 parent 6c297aa commit 2c77bd7

2 files changed

Lines changed: 69 additions & 194 deletions

File tree

examples/src/BasicSynchronization.cpp

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "tbb/tick_count.h"
1010

1111
#include "react/Domain.h"
12+
#include "react/Signal.h"
1213
#include "react/Event.h"
1314
#include "react/Observer.h"
1415

@@ -37,7 +38,7 @@ namespace example1
3738
Sensor mySensor;
3839

3940
Observe(mySensor.Samples, [] (int v) {
40-
cout << v << std::endl;
41+
cout << v << endl;
4142
});
4243

4344
TransactionStatus status;
@@ -157,14 +158,79 @@ namespace example2
157158
}
158159
}
159160

161+
///////////////////////////////////////////////////////////////////////////////////////////////////
162+
/// Example 3 - Continuations (1)
163+
///////////////////////////////////////////////////////////////////////////////////////////////////
164+
namespace example3
165+
{
166+
using namespace react;
167+
using namespace std;
168+
169+
REACTIVE_DOMAIN(D, sequential_concurrent)
170+
171+
class Widget
172+
{
173+
public:
174+
USING_REACTIVE_DOMAIN(D)
175+
176+
VarSignalT<string> Label1 = MakeVar<D>(string( "Change" ));;
177+
VarSignalT<string> Label2 = MakeVar<D>(string( "me!" ));;
178+
179+
EventSourceT<> Reset = MakeEventSource<D,Token>();
180+
181+
Widget() :
182+
resetCont_
183+
(
184+
MakeContinuation<D>(
185+
Reset,
186+
[this] (Token) {
187+
Label1 <<= string( "Change" );
188+
Label2 <<= string( "me!" );
189+
})
190+
)
191+
{}
192+
193+
private:
194+
Continuation<D> resetCont_;
195+
};
196+
197+
void Run()
198+
{
199+
cout << "Example 3 - Continuations (1)" << endl;
200+
201+
Widget myWidget;
202+
int sum = 0;
203+
204+
Observe(myWidget.Label1, [&] (const string& v) {
205+
cout << "Label 1 changed to " << v << endl;;
206+
});
207+
208+
Observe(myWidget.Label2, [&] (const string& v) {
209+
cout << "Label 2 changed to " << v << endl;;
210+
});
211+
212+
myWidget.Label1 <<= "Hello";
213+
myWidget.Label2 <<= "world";
214+
215+
cout << "Resetting..." << endl;
216+
217+
myWidget.Reset();
218+
219+
cout << endl;
220+
}
221+
}
222+
160223
///////////////////////////////////////////////////////////////////////////////////////////////////
161224
/// Run examples
162225
///////////////////////////////////////////////////////////////////////////////////////////////////
163226
int main()
164227
{
165228
example1::Run();
229+
166230
example2::v1::Run();
167231
example2::v2::Run();
168232

233+
example3::Run();
234+
169235
return 0;
170236
}

examples/src/Main.cpp

Lines changed: 2 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -18,205 +18,14 @@
1818
using namespace std;
1919
using namespace react;
2020

21-
// Defines a domain.
22-
// Each domain represents a separate dependency graph, managed by a dedicated propagation engine.
23-
// Reactives of different domains can not be combined.
24-
25-
26-
void SignalExample3()
27-
{
28-
REACTIVE_DOMAIN(D, sequential_concurrent)
29-
30-
cout << "Signal Example 3" << endl;
31-
32-
auto src = MakeVar<D>(0);
33-
34-
// Input values can be manipulated imperatively in observers.
35-
// Inputs are implicitly thread-safe, buffered and executed in a continuation turn.
36-
// This continuation turn is queued just like a regular turn.
37-
// If other turns are already queued, they are executed before the continuation.
38-
auto cont = MakeContinuation(src, [&] (int v) {
39-
cout << "V: " << v << endl;
40-
if (v < 10)
41-
src <<= v+1;
42-
});
43-
44-
src <<= 1;
45-
46-
cout << endl;
47-
}
48-
49-
void SignalExample4()
50-
{
51-
REACTIVE_DOMAIN(L, sequential_concurrent, ToposortEngine)
52-
REACTIVE_DOMAIN(R, sequential_concurrent, ToposortEngine)
53-
54-
cout << "Signal Example 4" << endl;
55-
56-
auto srcL = MakeVar<L>(0);
57-
auto srcR = MakeVar<R>(0);
58-
59-
auto contL = MakeContinuation<L,R>(srcL, [&] (int v) {
60-
cout << "L->R: " << v << endl;
61-
if (v < 10)
62-
srcR <<= v+1;
63-
});
64-
65-
auto contR = MakeContinuation<R,L>(srcR, [&] (int v) {
66-
cout << "R->L: " << v << endl;
67-
if (v < 10)
68-
srcL <<= v+1;
69-
});
70-
71-
srcL <<= 1;
72-
printf("End\n");
73-
74-
cout << endl;
75-
}
76-
77-
void SignalExample5()
78-
{
79-
REACTIVE_DOMAIN(L, sequential_concurrent, ToposortEngine)
80-
REACTIVE_DOMAIN(R, sequential_concurrent, ToposortEngine)
81-
82-
cout << "Signal Example 5" << endl;
83-
84-
auto srcL = MakeVar<L>(0);
85-
auto depL1 = MakeVar<L>(0);
86-
auto depL2 = MakeVar<L>(0);
87-
auto srcR = MakeVar<R>(0);
88-
89-
auto contL = MakeContinuation<L,R>(
90-
Monitor(srcL),
91-
With(depL1, depL2),
92-
[&] (int v, int depL1, int depL2) {
93-
cout << "L->R: " << v << endl;
94-
if (v < 10)
95-
srcR <<= v+1;
96-
});
97-
98-
auto contR = MakeContinuation<R,L>(
99-
Monitor(srcR),
100-
[&] (int v) {
101-
cout << "R->L: " << v << endl;
102-
if (v < 10)
103-
srcL <<= v+1;
104-
});
105-
106-
srcL <<= 1;
107-
printf("End\n");
108-
109-
cout << endl;
110-
}
111-
11221
void testme()
11322
{
114-
REACTIVE_DOMAIN(D, sequential_concurrent)
115-
116-
std::vector<int> results;
117-
118-
auto f_0 = [] (int a) -> int
119-
{
120-
int k = 0;
121-
for (int i = 0; i<10000; i++)
122-
k += i;
123-
return a + k;
124-
};
125-
126-
auto f_n = [] (int a, int b) -> int
127-
{
128-
int k = 0;
129-
for (int i=0; i<10000; i++)
130-
k += i;
131-
return a + b + k;
132-
};
133-
134-
auto n1 = MakeVar<D>(0);
135-
auto n2 = n1 ->* f_0;
136-
auto n3 = ((n2, n1) ->* f_n) ->* f_0;
137-
auto n4 = n3 ->* f_0;
138-
auto n5 = ((((n4, n3) ->* f_n), n1) ->* f_n) ->* f_0;
139-
auto n6 = n5 ->* f_0;
140-
auto n7 = ((n6, n5) ->* f_n) ->* f_0;
141-
auto n8 = n7 ->* f_0;
142-
auto n9 = ((((((n8, n7) ->* f_n), n5) ->* f_n), n1) ->* f_n) ->* f_0;
143-
auto n10 = n9 ->* f_0;
144-
auto n11 = ((n10, n9) ->* f_n) ->* f_0;
145-
auto n12 = n11 ->* f_0;
146-
auto n13 = ((((n12, n11) ->* f_n), n9) ->* f_n) ->* f_0;
147-
auto n14 = n13 ->* f_0;
148-
auto n15 = ((n14, n13) ->* f_n) ->* f_0;
149-
auto n16 = n15 ->* f_0;
150-
auto n17 = ((((((n16, n15) ->* f_n), n13) ->* f_n), n9) ->* f_n) ->* f_0;
151-
152-
auto src = MakeEventSource<D,int>();
153-
154-
atomic<int> c( 0 );
155-
156-
Observe(src, [&] (int v){
157-
c++;
158-
});
159-
160-
auto x0 = tbb::tick_count::now();
161-
162-
TransactionStatus st;
163-
164-
for (int i=0; i<10000; i++)
165-
{
166-
AsyncTransaction<D>(st, [&,i] {
167-
n1 <<= 1+i;
168-
});
169-
}
170-
171-
for (int i=0; i<10000; i++)
172-
{
173-
AsyncTransaction<D>(st, [&,i] {
174-
n1 <<= 20000+i;
175-
});
176-
}
177-
178-
for (int i=0; i<10000; i++)
179-
{
180-
AsyncTransaction<D>(st, [&,i] {
181-
n1 <<= 100000+i;
182-
});
183-
}
184-
185-
st.Wait();
186-
187-
//std::thread t3([&] {
188-
// for (int i=0; i<10000; i++)
189-
// n1 <<= 1+i;
190-
//});
191-
192-
//std::thread t2([&] {
193-
// for (int i=0; i<10000; i++)
194-
// n1 <<= 20000+i;
195-
//});
196-
197-
//std::thread t1([&] {
198-
// for (int i=0; i<10000; i++)
199-
// n1 <<= 100000+i;
200-
//});
201-
202-
//t3.join();
203-
//t2.join();
204-
//t1.join();
205-
//std::chrono::milliseconds dura( 10000 );
206-
//std::this_thread::sleep_for( dura );
207-
208-
auto x1 = tbb::tick_count::now();
209-
210-
double d = (x1 - x0).seconds();
211-
printf("Time %g\n", d);
212-
printf("Updates %d\n", c.load());
213-
printf("n1 %d\n", n1());
23+
// Note: This project exists as a sandbox where I occasionally stage new examples.
24+
// Currently it's empty.
21425
}
21526

21627
int main()
21728
{
218-
SignalExample3();
219-
SignalExample4();
22029
testme();
22130

22231
#ifdef REACT_ENABLE_LOGGING

0 commit comments

Comments
 (0)