Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonbit/coroutine
C++11 single .h asymmetric coroutine implementation via ucontext / fiber
https://github.com/tonbit/coroutine
Last synced: 3 months ago
JSON representation
C++11 single .h asymmetric coroutine implementation via ucontext / fiber
- Host: GitHub
- URL: https://github.com/tonbit/coroutine
- Owner: tonbit
- License: apache-2.0
- Created: 2016-09-09T13:29:53.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-12-20T14:11:17.000Z (almost 3 years ago)
- Last Synced: 2024-05-02T15:25:56.722Z (6 months ago)
- Language: C++
- Homepage:
- Size: 19.5 KB
- Stars: 409
- Watchers: 29
- Forks: 93
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- AwesomeCppGameDev - coroutine
README
# C++11 single .h asymmetric coroutine implementation
### API
in namespace coroutine:
* routine_t create(std::function f);
* void destroy(routine_t id);
* int resume(routine_t id);
* void yield();
* TYPE await(TYPE(*f)());
* routine_t current();
* class Channel with push()/pop();### OS
* Linux
* macOS
* Windows### Demo
```cpp
#include "coroutine.h"
#include
#includecoroutine::Channel channel;
string async_func()
{
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
return "22";
}void routine_func1()
{
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;
}void thread_func()
{
//create routine with callback like std::function
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;
}
```