|
18 | 18 | using namespace std; |
19 | 19 | using namespace react; |
20 | 20 |
|
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 | | - |
112 | 21 | void testme() |
113 | 22 | { |
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. |
214 | 25 | } |
215 | 26 |
|
216 | 27 | int main() |
217 | 28 | { |
218 | | - SignalExample3(); |
219 | | - SignalExample4(); |
220 | 29 | testme(); |
221 | 30 |
|
222 | 31 | #ifdef REACT_ENABLE_LOGGING |
|
0 commit comments