Last change
on this file since 830edc6 was
a573c22,
checked in by Peter A. Buhr <pabuhr@…>, 5 years ago
|
example programs updated for concurrency paper
|
-
Property mode set to
100644
|
File size:
908 bytes
|
Line | |
---|
1 | _Coroutine Format { |
---|
2 | char ch; // used for communication |
---|
3 | int g, b; // global because used in destructor |
---|
4 | void main() { |
---|
5 | for ( ;; ) { // for as many characters |
---|
6 | for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks |
---|
7 | for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters |
---|
8 | for ( ;; ) { // for newline characters |
---|
9 | suspend(); |
---|
10 | if ( ch != '\n' ) break; // ignore newline |
---|
11 | } |
---|
12 | // cout << ch; // print character |
---|
13 | } |
---|
14 | // cout << " "; // print block separator |
---|
15 | } |
---|
16 | // cout << endl; // print group separator |
---|
17 | } |
---|
18 | } |
---|
19 | public: |
---|
20 | Format() { resume(); } // start coroutine |
---|
21 | // ~Format() { if ( g != 0 | | b != 0 ) cout << endl; } |
---|
22 | void prt( char ch ) { Format::ch = ch; resume(); } |
---|
23 | }; |
---|
24 | |
---|
25 | int main() { |
---|
26 | Format fmt; |
---|
27 | for ( long int i = 0; i < 1000000000; i += 1 ) |
---|
28 | fmt.prt( 'a' ); |
---|
29 | } |
---|
30 | |
---|
31 | // Local Variables: // |
---|
32 | // tab-width: 4 // |
---|
33 | // compile-command: "u++-work -O2 -nodebug Format.cc" // |
---|
34 | // End: // |
---|
Note: See
TracBrowser
for help on using the repository browser.