Giter Site home page Giter Site logo

cpp-coroutines's Introduction

C++ Coroutines

Most code in this repo is copied to the clu project, thus this repo is archived.

Test things out and try to learn C++20 coroutines.

  • concepts.h: Basic coroutine concepts.

    static_assert(clu::awaiter<clu::task<>>);
    static_assert(clu::awaiter_of<clu::task<int>, int>);
    static_assert(std::same_as<clu::awaiter_result_t<clu::task<int>&&>, int&&>);
  • generator.h: Generator type generator<T>, supports co_yield operations.

    clu::generator<int> fib()
    {
        int a = 1, b = 0;
        while (true)
        {
            a = std::exchange(b, a + b);
            co_yield b;
        }
    }
    static_assert(std::ranges::input_range<clu::generator<int>>);
  • schedule.h: Defines scheduler concept and provides a generic schedule_on function template for scheduling tasks.

    static_assert(clu::scheduler<clu::static_thread_pool>);
    
    clu::task<> scheduled_task(clu::scheduler auto& sch)
    {
        co_await clu::schedule_on(sch);
        std::cout << "On scheduler!\n";
    }
  • spawn.h: Spawn a detached coroutine with spawn(awaitable).

    void non_coroutine()
    {
        spawn(some_task()); // Be careful that the task is detached
        do_other_things();
    }
  • static_thread_pool.h / .cpp: Provides a simple static thread pool implementation static_thread_pool. The thread pool type satisfies scheduler.

    clu::static_thread_pool pool;
    
    clu::task<> task()
    {
        std::cout << "On original thread!\n";
        co_await pool.schedule();
        std::cout << "Now I'm on the thread pool!\n";
    }
  • sync_wait.h: Wait for an awaitable's completion on the current thread and retrieve the result using sync_wait.

    clu::task<> task()
    {
        std::this_thread::sleep_for(1s);
        co_return 42;
    }
    
    int main()
    {
        const int answer = sync_wait(task());
        std::cout << "The answer is " << answer << '\n';
    }
  • task.h: Lazy task type task<T>, supports co_await operations.

    clu::task<> some_task()
    {
        const int result = co_await async_long_task();
        co_return 42 + result;
    }

cpp-coroutines's People

Contributors

chlorie avatar

Watchers

 avatar

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.