| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6c297aa commit 2c77bd7
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | #include "tbb/tick_count.h" | |
| 10 | 10 | ||
| 11 | 11 | #include "react/Domain.h" | |
| 12 | + #include "react/Signal.h" | ||
| 12 | 13 | #include "react/Event.h" | |
| 13 | 14 | #include "react/Observer.h" | |
| 14 | 15 | ||
@@ -37,7 +38,7 @@ namespace example1 | |||
| 37 | 38 | Sensor mySensor; | |
| 38 | 39 | ||
| 39 | 40 | Observe(mySensor.Samples, [] (int v) { | |
| 40 | - cout << v << std::endl; | ||
| 41 | + cout << v << endl; | ||
| 41 | 42 | }); | |
| 42 | 43 | ||
| 43 | 44 | TransactionStatus status; | |
@@ -157,14 +158,79 @@ namespace example2 | |||
| 157 | 158 | } | |
| 158 | 159 | } | |
| 159 | 160 | ||
| 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 | + | ||
| 160 | 223 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 161 | 224 | /// Run examples | |
| 162 | 225 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 163 | 226 | int main() | |
| 164 | 227 | { | |
| 165 | 228 | example1::Run(); | |
| 229 | + | ||
| 166 | 230 | example2::v1::Run(); | |
| 167 | 231 | example2::v2::Run(); | |
| 168 | 232 | ||
| 233 | + example3::Run(); | ||
| 234 | + | ||
| 169 | 235 | return 0; | |
| 170 | 236 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,205 +18,14 @@ | |||
| 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 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments