AsyncIO is a C++ coroutine helper library based on llvm-5.0's (or above) coroutine feature. AsycnIO v0.2 has two components:
- Couroutine Coroutine makes it much easier to write coroutine methods, generators and asynchronous generators. These part of AsyncIO is header only that means if this is all you need, you can just download the source code into your projects, then use it without build AsyncIO
- EventLoop EventLoop is a simple scheduler based on libuv, inspired by EventLoop of Python3. it helps you run multiple coroutines "simultaneously" within a thread.
Stage | OSX |
---|---|
Unit Test |
Make sure you have installed llvm 5.0 or above, libc++, libc++abi, libuv and cmake.
brew tap homebrew/versions
brew install --HEAD llvm #this will install libc++ by default
brew install cmake
brew install
sudo apt-get install libuv cmake
# install llvm-5.0 libc++ libc++abi to /usr/local
# download from here http://releases.llvm.org/download.html#5.0.0
cd $ASYNCIO_PATH && mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j 10
make test
make install
There are some examples in asyncio/examples directory. For more detail infomation about specific class, you can check the tests in asyncio/tests directory or just have a look at the code.
Source Code: sleep_sort.cpp
#include <asyncio/asyncio.hpp>
#include <iostream>
using namespace std;
using namespace asyncio;
coro<void> sleepCout(EventLoop *loop, uint64_t value) {
co_await sleep(loop, value);
cout << value << endl;
}
int main() {
EventLoop loop;
cout << "Someone says 'sleep sort' is O(1)?" << endl;
uint64_t max = -1;
for (auto &&v : {5, 1, 9, 7, 3}) {
loop.createTask(sleepCout(&loop, v))->release();
max = max > v ? max : v;
}
loop.callLater(max + 20, [&] { loop.stop(); })->release();
loop.runForever();
cout << "... I think it makes sense! 🤣🤣🤣" << endl;
}
compile it
$ clang -I $ASYNCIO_HEADER_PATH -o sleep_sort -lc++ -std=c++14 -stdlib=libc++ -fcoroutines-ts -lasyncio -L $ASYNCIO_LIB_PATH -rpath $ASYNCIO_LIB_PATH sleep_sort.cpp
$ ./sleep_sort
Someone says 'sleep sort' is O(1)?
1
3
5
7
9
... I think it makes sense! 🤣🤣🤣