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 separating it. I haven't moved the net/socket stuff yet, but check out kit's net.h sockets to see how to use this in that context.

It's "single header" but requires you link to some boost libs.

This is a prototype. There are still issues with stability and lack of features, maybe even thread safety. You've been warned! This should serve as a proof-of-concept regardless.

Here are the basics: there is a singleton i/o multiplexer (i.e. "MX") which is initialized on first usage. The number of circuits is by default set to your core count, but wraps back around beyond that.

You give the "circuit number" you wish to operate on. This index is a hint on how the separate the tasks across cores. Because of the wraping of circuit indices, numbers that are closer together are more likely to be put on a different cores.

You can use local_shared_ptrs if you want, since each circuit has its own thread.

If you're familiar with Go, the mx's "coro" method (accessed by MX[0].coro<void> below) is similar to using "go".

// on circuit 0, launch coroutine, return future<void>
auto fut = MX[0].coro<void>([]{
    // do async stuff
    auto foo = MX_AWAIT(bar);

    // async sleep yield
    MX_SLEEP(chrono::milliseconds(100));
});

// use fut here
// on circuit 0, launch coroutine, using kit's sockets, which is also a prototype implementation
MX[0].coro<void>([&]{
    for(;;)
    {
        auto client = make_local_shared<TCPSocket>(MX_AWAIT(server->accept()));
        
        // coroutine per client
        MX[0].coro<void>([&, client]{
            int client_id = client_ids++;
            cout << "client " << client_id << " connected" << endl;
            try{
                for(;;)
                    MX_AWAIT(client->send(MX_AWAIT(client->recv())));
            }catch(const socket_exception& e){
            }
        });
    }
});

I don't have documentation of all features, check out mx.h if you're brave.

Future

Currently, you can await more than one future in a circuit, but this causes a polling situation that requires a stabizing timer to prevent wasting the CPU. The next thing to add would be utilizing a shared method of polling (such as libuv) and responding to user futures through callbacks instead of being polled.

LICENSE

Open-source under MIT License.

See LICENSE file for details.

Copyright (c) Grady O'Connell, 2013

Owner
Grady O'Connell
Game & Software Developer, Musician
Grady O'Connell
Similar Resources

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

A fast single-producer, single-consumer lock-free queue for C++

A single-producer, single-consumer lock-free queue for C++ This mini-repository has my very own implementation of a lock-free queue (that I designed f

Dec 3, 2022

A bounded single-producer single-consumer wait-free and lock-free queue written in C++11

A bounded single-producer single-consumer wait-free and lock-free queue written in C++11

SPSCQueue.h A single producer single consumer wait-free and lock-free fixed size queue written in C++11. Example SPSCQueueint q(2); auto t = std::th

Dec 1, 2022

C++20's jthread for C++11 and later in a single-file header-only library

C++20's jthread for C++11 and later in a single-file header-only library

jthread lite: C++20's jthread for C++11 and later A work in its infancy. Suggested by Peter Featherstone. Contents Example usage In a nutshell License

Nov 26, 2022

Px - Single header C++ Libraries for Thread Scheduling, Rendering, and so on...

px 'PpluX' Single header C++(11/14) Libraries Name Code Description px_sched px_sched.h Task oriented scheduler. See more px_render px_render.h Multit

Nov 25, 2022

Coro - Single-header library facilities for C++2a Coroutines

coro This is a collection of single-header library facilities for C++2a Coroutines. coro/include/ co_future.h Provides co_futureT, which is like std

Nov 15, 2022

A competitive programming helper tool, which packages included libraries into a single file, suitable for online judges.

cpack Cpack is a competitive programming helper tool, which packages the main source file along with included libraries into a single file, suitable f

Apr 22, 2022

Fork of rpmalloc to be used with single thread applications and old C compilers

srpmalloc - Small rpmalloc This is a fork of rpmalloc, with the intent to be used in single threaded applications only, with old C99 compilers, and in

Oct 28, 2022

Xenium is a noninteractive protocol for producing and redeeming single-use claim codes

Xenium Xenium is a noninteractive protocol for producing and redeeming single-use claim codes. What this means in practice is that you can have a devi

Sep 24, 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
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
: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
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
A C++20 coroutine library based off asyncio
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

Nov 9, 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
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
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
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
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