source:
doc/papers/concurrency/examples/Pingpong.cc@
e3d4fd1
| Last change on this file since e3d4fd1 was 17c6c1c3, checked in by , 7 years ago | |
|---|---|
|
|
| File size: 807 bytes | |
| Line | |
|---|---|
| 1 | #include <iostream> |
| 2 | using namespace std; |
| 3 | |
| 4 | _Coroutine PingPong { |
| 5 | const char * name; |
| 6 | const unsigned int N; |
| 7 | PingPong * part; |
| 8 | void main() { // ping’s starter ::main, pong’s starter ping |
| 9 | for ( unsigned int i = 0; i < N; i += 1 ) { |
| 10 | cout << name << endl; |
| 11 | part->cycle(); |
| 12 | } |
| 13 | } |
| 14 | public: |
| 15 | PingPong( const char * name, unsigned int N, PingPong & part ) |
| 16 | : name( name ), N( N ), part( & part ) {} |
| 17 | PingPong( const char * name, unsigned int N ) : name( name ), N( N ) {} |
| 18 | void partner( PingPong & part ) { PingPong::part = ∂ } |
| 19 | void cycle() { resume(); } |
| 20 | }; |
| 21 | int main() { |
| 22 | enum { N = 10 }; |
| 23 | PingPong ping( "ping", N ), pong( "pong", N, ping ); |
| 24 | ping.partner( pong ); |
| 25 | ping.cycle(); |
| 26 | } |
| 27 | |
| 28 | // Local Variables: // |
| 29 | // tab-width: 4 // |
| 30 | // compile-command: "u++-work -O2 -nodebug Pingpong.cc" // |
| 31 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.