Changeset ae6b6cf
- Timestamp:
- Mar 26, 2019, 2:12:57 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 8dbfb7e
- Parents:
- 1bb2488
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/examples/ProdCons.cpp
r1bb2488 rae6b6cf 12 12 struct Cons; 13 13 14 struct Prod { 14 struct resumable { 15 virtual resumable * resume() = 0; 16 }; 17 18 struct Prod : public resumable { 15 19 struct local { 16 20 Cons * c; … … 20 24 struct promise_type { 21 25 local _l; 26 resumable * next; 22 27 23 28 Prod get_return_object() { … … 69 74 static Prod main(); 70 75 71 auto payment(int money) { 72 _coroutine.promise()._l.money = money; 73 struct ret { 74 int _receipt; 75 bool await_ready() { return false; } 76 void await_suspend(std::experimental::coroutine_handle<>) {} 77 int await_resume() { return _receipt; } 78 }; 79 return ret{ _coroutine.promise()._l.receipt }; 80 } 76 struct payment_return; 77 78 payment_return payment(int money); 81 79 82 80 auto start(int N, Cons & c) { … … 84 82 _coroutine.promise()._l.N = N; 85 83 _coroutine.promise()._l.receipt = 0; 84 } 85 86 virtual resumable * resume() override final { 86 87 _coroutine.resume(); 87 } 88 }; 89 90 struct Cons { 88 return _coroutine.promise().next; 89 } 90 }; 91 92 struct Cons : public resumable { 91 93 struct local { 92 94 Prod * p; … … 97 99 struct promise_type { 98 100 local _l; 101 resumable * next; 99 102 100 103 Cons get_return_object() { … … 154 157 struct ret { 155 158 int _status; 159 Cons * c; 156 160 bool await_ready() { return false; } 157 void await_suspend(std::experimental::coroutine_handle<>) {} 161 void await_suspend(std::experimental::coroutine_handle<Prod::promise_type> _coroutine) { 162 _coroutine.promise().next = c; 163 } 158 164 int await_resume() { return _status; } 159 165 }; 160 return ret{ _coroutine.promise()._l.status };166 return ret{ _coroutine.promise()._l.status, this }; 161 167 } 162 168 … … 164 170 _coroutine.promise()._l.done = true; 165 171 struct ret { 172 Cons * c; 173 Prod::promise_type * _promise; 166 174 bool await_ready() { return false; } 167 void await_suspend(std::experimental::coroutine_handle<>) {} 168 void await_resume() {} 175 void await_suspend(std::experimental::coroutine_handle<Prod::promise_type> _coroutine) { 176 _promise = &_coroutine.promise(); 177 _promise->next = c; 178 } 179 void await_resume() { 180 _promise->next = nullptr; 181 } 169 182 }; 170 return ret{}; 171 } 172 }; 183 return ret{this, nullptr}; 184 } 185 186 virtual resumable * resume() override final { 187 _coroutine.resume(); 188 return _coroutine.promise().next; 189 } 190 }; 191 192 struct Prod::payment_return { 193 int _receipt; 194 Prod * p; 195 bool await_ready() { return false; } 196 void await_suspend(std::experimental::coroutine_handle<Cons::promise_type> _coroutine) { 197 _coroutine.promise().next = p; 198 } 199 int await_resume() { return _receipt; } 200 }; 201 202 Prod::payment_return Prod::payment(int money) { 203 _coroutine.promise()._l.money = money; 204 return payment_return{ _coroutine.promise()._l.receipt, this }; 205 } 173 206 174 207 Prod Prod::main() { … … 176 209 for(int i = 0; i < p.N; i++) { 177 210 int p1 = random(100), p2 = random(100); 178 std::cout << p1 << " " << p2 ;211 std::cout << p1 << " " << p2 << std::endl; 179 212 int status = co_await p.c->deliver(p1, p2); 180 std::cout << " $" << p.money << std::endl << status ;213 std::cout << " $" << p.money << std::endl << status << std::endl; 181 214 p.receipt += 1; 182 215 } 183 216 co_await p.c->stop(); 184 std::cout << "prod stops" ;217 std::cout << "prod stops" << std::endl; 185 218 } 186 219 … … 189 222 int money = 1, receipt; 190 223 for(;!c.done ;) { 191 std::cout << c.p1 << " " << c.p2 << std::endl << " $" << money; 224 std::cout << c.p1 << " " << c.p2 << std::endl; 225 std::cout << " $ " << money << std::endl; 192 226 c.status += 1; 193 227 receipt = co_await c.p->payment( money ); 194 std::cout << " # " << receipt;228 std::cout << " # " << receipt << std::endl; 195 229 money += 1; 196 230 } 197 std::cout << "const stops"; 231 std::cout << "cons stops" << std::endl; 232 } 233 234 void dispatch(resumable * r) { 235 while((r = r->resume())); 198 236 } 199 237 … … 203 241 srandom( getpid() ); 204 242 prod.start(5, cons); 205 } 243 dispatch(&prod); 244 }
Note: See TracChangeset
for help on using the changeset viewer.