Coder Social home page Coder Social logo

basic-coro's Introduction

basic-coro - c++ coroutine library

Library that implements helper types for using c++ coroutines. Please be aware that this is a training project for me - I wanted to learn more about CMake, gtest and git submodules.

Usage

Prerequisites

  • g++-10

Installing

mkdir build && cd build
cmake -D CMAKE_CXX_COMPILER=g++-10 ..
make install

This will install appropriate headers into ./include/ and static linked library into ./lib/.

Classes

Library includes following classes:

  • SingleEvent<T> which models co_await enabled event that can be set,
  • AwaitableTask<T> which models co_await enabled task.

Please note that these classes are not multithreading enabled. There is no synchronization or any kind of protection form race conditions. If you need to use coroutines with multithreading, just use CppCoro. This library is mostly thought for use with simple GUI programming.

Example

#include <iostream>

#include <basiccoro/AwaitableTask.hpp>
#include <basiccoro/SingleEvent.hpp>

basiccoro::AwaitableTask<void> consumer(basiccoro::SingleEvent<int>& event)
{
    std::cout << "consumer: start waiting" << std::endl;

    while (true)
    {
        const auto i = co_await event;
        std::cout << "consumer: received: " << i << std::endl;
    }
}

int main()
{
    basiccoro::SingleEvent<int> event;
    consumer(event);

    while (true)
    {
        int i = 0;

        std::cout << "Enter no.(1-9): ";
        std::cin >> i;

        if (i == 0)
        {
            break;
        }
        else if (1 <= i && i <= 9)
        {
            event.set(i);
        }
    }
}

Simple example highlighting use of coroutines in producer-consumer problem.

Acknowledgments

  • CMake C++ Project Template as this project is based on this template
  • Lewis Baker has excellent articles on topic of coroutines and assymetric transfer. This project is mostly based on information (and code snippets) contained in those articles.

basic-coro's People

Contributors

kadukows avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

niansa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.