Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix markdown warnings #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 63 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# C++11 single .h asymmetric coroutine implementation

### API
## API

in namespace coroutine:
* routine_t create(std::function<void()> f);
* void destroy(routine_t id);
* int resume(routine_t id);
* void yield();
* TYPE await(TYPE(*f)());
* routine_t current();
* class Channel<T> with push()/pop();
in namespace coroutine:

### OS
* `routine_t create(std::function<void()> f);`
* `void destroy(routine_t id);`
* `int resume(routine_t id);`
* `void yield();`
* `TYPE await(TYPE(*f)());`
* `routine_t current();`
* `class Channel<T>` with `push()`/`pop()`;

## OS

* Linux
* macOS
* Windows

### Demo
## Demo

```cpp
#include "coroutine.h"
#include <iostream>
Expand All @@ -29,71 +30,71 @@ coroutine::Channel<int> channel;
string async_func()
{
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
return "22";
return "22";
}

void routine_func1()
{
int i = channel.pop();
std::cout << i << std::endl;
i = channel.pop();
std::cout << i << std::endl;
int i = channel.pop();
std::cout << i << std::endl;

i = channel.pop();
std::cout << i << std::endl;
}

void routine_func2(int i)
{
std::cout << "20" << std::endl;
coroutine::yield();
std::cout << "21" << std::endl;

//run function async
//yield current routine if result not returned
string str = coroutine::await(async_func);
std::cout << str << std::endl;
std::cout << "20" << std::endl;
coroutine::yield();

std::cout << "21" << std::endl;

//run function async
//yield current routine if result not returned
string str = coroutine::await(async_func);
std::cout << str << std::endl;
}

void thread_func()
{
//create routine with callback like std::function<void()>
coroutine::routine_t rt1 = coroutine::create(routine_func1);
coroutine::routine_t rt2 = coroutine::create(std::bind(routine_func2, 2));
std::cout << "00" << std::endl;
coroutine::resume(rt1);

std::cout << "01" << std::endl;
coroutine::resume(rt2);
std::cout << "02" << std::endl;
channel.push(10);
std::cout << "03" << std::endl;
coroutine::resume(rt2);
std::cout << "04" << std::endl;
channel.push(11);
std::cout << "05" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(6000));
coroutine::resume(rt2);

//destroy routine, free resouce allocated
//Warning: don't destroy routine by itself
coroutine::destroy(rt1);
coroutine::destroy(rt2);
//create routine with callback like std::function<void()>
coroutine::routine_t rt1 = coroutine::create(routine_func1);
coroutine::routine_t rt2 = coroutine::create(std::bind(routine_func2, 2));

std::cout << "00" << std::endl;
coroutine::resume(rt1);

std::cout << "01" << std::endl;
coroutine::resume(rt2);

std::cout << "02" << std::endl;
channel.push(10);

std::cout << "03" << std::endl;
coroutine::resume(rt2);

std::cout << "04" << std::endl;
channel.push(11);

std::cout << "05" << std::endl;

std::this_thread::sleep_for(std::chrono::milliseconds(6000));
coroutine::resume(rt2);

//destroy routine, free resouce allocated
//Warning: don't destroy routine by itself
coroutine::destroy(rt1);
coroutine::destroy(rt2);
}

int main()
{
std::thread t1(thread_func);
std::thread t2([](){
//unsupport coordinating routine crossing threads
});
t1.join();
t2.join();
return 0;
std::thread t1(thread_func);
std::thread t2([](){
//unsupport coordinating routine crossing threads
});
t1.join();
t2.join();
return 0;
}
```