| 1 | #include <functional>
|
|---|
| 2 | #include <iostream>
|
|---|
| 3 | #include <boost/coroutine/all.hpp>
|
|---|
| 4 | #include <stdlib.h>
|
|---|
| 5 | #include <new>
|
|---|
| 6 | #include <stdio.h>
|
|---|
| 7 |
|
|---|
| 8 | using namespace std;
|
|---|
| 9 | using namespace std::placeholders;
|
|---|
| 10 | using namespace boost::coroutines;
|
|---|
| 11 |
|
|---|
| 12 | void* operator new (size_t size) {
|
|---|
| 13 | void *ptr = malloc(size);
|
|---|
| 14 |
|
|---|
| 15 | printf("Allocated: %p\n", ptr);
|
|---|
| 16 |
|
|---|
| 17 | return ptr;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | void operator delete (void *ptr) {
|
|---|
| 21 | printf("Freeing: %p\n", ptr);
|
|---|
| 22 |
|
|---|
| 23 | free(ptr);
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | struct XX {
|
|---|
| 27 |
|
|---|
| 28 | static void Hello(coroutine<void(int)>::caller_type& ca) {
|
|---|
| 29 | cout << "In Hello " << ca.get() << endl;
|
|---|
| 30 | ca();
|
|---|
| 31 | cout << "In Hello end " << ca.get() << endl;
|
|---|
| 32 | ca();
|
|---|
| 33 | cout << "In Hello end " << ca.get() << endl;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | void Run() {
|
|---|
| 37 | coroutine<void(int)> coro(Hello, 6);
|
|---|
| 38 |
|
|---|
| 39 | cout << "In Run1" << endl;
|
|---|
| 40 | int x = 4;
|
|---|
| 41 | while (coro) {
|
|---|
| 42 | coro(x++);
|
|---|
| 43 | }
|
|---|
| 44 | cout << "In Run2" << endl;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | int main(int argc, char **argv) {
|
|---|
| 50 | printf("Starting main\n");
|
|---|
| 51 |
|
|---|
| 52 | XX *xx = new XX;
|
|---|
| 53 | xx->Run();
|
|---|
| 54 | delete xx;
|
|---|
| 55 |
|
|---|
| 56 | printf("Finished main\n");
|
|---|
| 57 |
|
|---|
| 58 | return 0;
|
|---|
| 59 | }
|
|---|