A C++20 coroutine library based off asyncio

kuro

A C++20 coroutine library, somewhat modelled on Python's asyncio

Requirements

Kuro requires a C++20 compliant compiler and a Linux OS. Tested on GCC 10.2 / Ubuntu 20.04.

Example

Start a TCP server, and add all the integers received until the user stops the program.

#include <iostream>
#include <kuro/kuro.hpp>

kuro::cancellation cancel;
kuro::queue<int> sum_queue;

void stop_server()
{
    std::cout << "Stopping server" << std::endl;
    cancel.trigger();
}

kuro::task<void> handle_connection(kuro::ipv4_socket sock)
{
    char msg[100];
    std::optional<ssize_t> n_bytes_recv = co_await kuro::with_timeout(
        sock.recv(msg, 100), 
        std::chrono::seconds(10)
    );

    if (n_bytes_recv) {
        std::string_view reply = "Message received";
        co_await sock.send(reply.data(), reply.size());

        int value = std::stoi(std::string(msg, *n_bytes_recv));
        sum_queue.push(value);
    } else {
        std::cout << "Client timed out" << std::endl;
    }
}

kuro::task<int> sum()
{
    int total = 0;
    while (true) {
        std::optional<int> value = co_await kuro::with_cancellation(
            sum_queue.pop(),
            cancel
        );

        if (!value) {
            co_return total;
        }
        total += *value;
    }
}

kuro::task<void> app_main()
{
    kuro::event_loop::add_signal_handler(SIGINT, stop_server);

    auto sum_task = kuro::event_loop::create_task(sum());

    auto sock = kuro::tcpv4_listen_socket(8001);
    co_await sock.serve_forever(handle_connection, cancel);

    std::cout << "Server stopped" << std::endl;
    std::cout << "Sum is: " << co_await sum_task << std::endl;
}

int main()
{
    kuro::event_loop::run(app_main());
}

Disclaimer

This code is super-alpha quality. It's barely tested beyond writing simple examples, and is basically a hastily thrown together mess. If you want to get started with C++20 coroutines, you'll be better off using cppcoro, or waiting until libunifex is more mature. Still, coroutines are a very complex and flexible language feature, and I hope looking at my code will help you understand how to use and implement coroutines in your own code.

Similar Resources

Mx - C++ coroutine await, yield, channels, i/o events (single header + link to boost)

mx C++11 coroutine await, yield, channels, i/o events (single header + link to boost). This was originally part of my c++ util library kit, but I'm se

Sep 21, 2019

Termite-jobs - Fast, multiplatform fiber based job dispatcher based on Naughty Dogs' GDC2015 talk.

NOTE This library is obsolete and may contain bugs. For maintained version checkout sx library. until I rip it from there and make a proper single-hea

Jan 9, 2022

A library for enabling task-based multi-threading. It allows execution of task graphs with arbitrary dependencies.

Fiber Tasking Lib This is a library for enabling task-based multi-threading. It allows execution of task graphs with arbitrary dependencies. Dependenc

Nov 18, 2022

OpenCL based GPU accelerated SPH fluid simulation library

libclsph An OpenCL based GPU accelerated SPH fluid simulation library Can I see it in action? Demo #1 Demo #2 Why? Libclsph was created to explore the

Jul 27, 2022

C++20 coroutines-based cooperative multitasking library

🐔 Coop Coop is a C++20 coroutines-based library to support cooperative multitasking in the context of a multithreaded application. The syntax will be

Dec 3, 2022

A C++17 message passing library based on MPI

MPL - A message passing library MPL is a message passing library written in C++17 based on the Message Passing Interface (MPI) standard. Since the C++

Nov 26, 2022

DwThreadPool - A simple, header-only, dependency-free, C++ 11 based ThreadPool library.

DwThreadPool - A simple, header-only, dependency-free, C++ 11 based ThreadPool library.

dwThreadPool A simple, header-only, dependency-free, C++ 11 based ThreadPool library. Features C++ 11 Minimal Source Code Header-only No external depe

Oct 28, 2022

Bolt is a C++ template library optimized for GPUs. Bolt provides high-performance library implementations for common algorithms such as scan, reduce, transform, and sort.

Bolt is a C++ template library optimized for heterogeneous computing. Bolt is designed to provide high-performance library implementations for common

Nov 27, 2022

oneAPI DPC++ Library (oneDPL) https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/dpc-library.html

oneAPI DPC++ Library (oneDPL) The oneAPI DPC++ Library (oneDPL) aims to work with the oneAPI DPC++ Compiler to provide high-productivity APIs to devel

Dec 3, 2022
C++20 Coroutine-Based Synchronous Parser Combinator Library

This library contains a monadic parser type and associated combinators that can be composed to create parsers using C++20 Coroutines.

Oct 13, 2022
C++14 coroutine-based task library for games

SquidTasks Squid::Tasks is a header-only C++14 coroutine-based task library for games. Full project and source code available at https://github.com/we

Nov 30, 2022
Elle - The Elle coroutine-based asynchronous C++ development framework.
Elle - The Elle coroutine-based asynchronous C++ development framework.

Elle, the coroutine-based asynchronous C++ development framework Elle is a collection of libraries, written in modern C++ (C++14). It contains a rich

Nov 19, 2022
:copyright: Concurrent Programming Library (Coroutine) for C11

libconcurrent tiny asymmetric-coroutine library. Description asymmetric-coroutine bidirectional communication by yield_value/resume_value native conte

Sep 2, 2022
Single header asymmetric stackful cross-platform coroutine library in pure C.
Single header asymmetric stackful cross-platform coroutine library in pure C.

minicoro Minicoro is single-file library for using asymmetric coroutines in C. The API is inspired by Lua coroutines but with C use in mind. The proje

Nov 30, 2022
A golang-style C++ coroutine library and more.

CO is an elegant and efficient C++ base library that supports Linux, Windows and Mac platforms. It pursues minimalism and efficiency, and does not rely on third-party library such as boost.

Dec 4, 2022
Cppcoro - A library of C++ coroutine abstractions for the coroutines TS

CppCoro - A coroutine library for C++ The 'cppcoro' library provides a large set of general-purpose primitives for making use of the coroutines TS pro

Nov 29, 2022
A go-style coroutine library in C++11 and more.
A go-style coroutine library in C++11 and more.

cocoyaxi English | 简体中文 A go-style coroutine library in C++11 and more. 0. Introduction cocoyaxi (co for short), is an elegant and efficient cross-pla

Dec 2, 2022
Powerful multi-threaded coroutine dispatcher and parallel execution engine

Quantum Library : A scalable C++ coroutine framework Quantum is a full-featured and powerful C++ framework build on top of the Boost coroutine library

Nov 28, 2022
Async GRPC with C++20 coroutine support

agrpc Build an elegant GRPC async interface with C++20 coroutine and libunifex (target for C++23 executor). Get started mkdir build && cd build conan

Nov 15, 2022