ADT
aaron-thesis
arm-eh
ast-experimental
cleanup-dtors
deferred_resn
demangler
enum
forall-pointer-decay
jacob/cs343-translation
jenkins-sandbox
new-ast
new-ast-unique-expr
new-env
no_list
persistent-indexer
pthread-emulation
qualifiedEnum
resolv-new
with_gc
|
Last change
on this file since f773f67 was 0e76cf4f, checked in by Thierry Delisle <tdelisle@…>, 9 years ago |
|
Updated the makefile for concurrency
|
-
Property mode
set to
100644
|
|
File size:
918 bytes
|
| Line | |
|---|
| 1 | #include <fstream>
|
|---|
| 2 | #include <threads>
|
|---|
| 3 |
|
|---|
| 4 | struct Fibonacci {
|
|---|
| 5 | coroutine c;
|
|---|
| 6 | int fn; // used for communication
|
|---|
| 7 | };
|
|---|
| 8 |
|
|---|
| 9 | void ?{}(Fibonacci* this) {
|
|---|
| 10 | this->fn = 0;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | coroutine* this_coroutine(Fibonacci* this) {
|
|---|
| 14 |
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | void co_main(Fibonacci* this) {
|
|---|
| 18 | int fn1, fn2; // retained between resumes
|
|---|
| 19 | this->fn = 0;
|
|---|
| 20 | fn1 = this->fn;
|
|---|
| 21 | suspend(this); // return to last resume
|
|---|
| 22 |
|
|---|
| 23 | this->fn = 1;
|
|---|
| 24 | fn2 = fn1;
|
|---|
| 25 | fn1 = this->fn;
|
|---|
| 26 | suspend(this); // return to last resume
|
|---|
| 27 |
|
|---|
| 28 | for ( ;; ) {
|
|---|
| 29 | this->fn = fn1 + fn2;
|
|---|
| 30 | fn2 = fn1;
|
|---|
| 31 | fn1 = this->fn;
|
|---|
| 32 | suspend(this); // return to last resume
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | int next(Fibonacci* this) {
|
|---|
| 37 | resume(this); // transfer to last suspend
|
|---|
| 38 | return this->fn;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | void main() {
|
|---|
| 42 | Fibonacci f1, f2;
|
|---|
| 43 | for ( int i = 1; i <= 10; i += 1 ) {
|
|---|
| 44 | sout | next(&f1) | ' ' | next(&f2) | endl;
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.