Skip to content

lightweight c++20 coroutine task class

License

Notifications You must be signed in to change notification settings

markandre13/async.cc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async.cc

a lightweight c++20 coroutine task class

"you can have peace. or you can have freedom. don't ever count on having both at once." -- robert a. heinlein

while c++20/23 coroutines provide a lot of freedom to accommodate many different use cases and hardware platforms, actually using them does not provide much peace of mind.

a lot of examples on the web do use a class named 'task' along with coroutines, but it's actually not part of the c++ standard.

this file contains a variation of cppcoro's 'task' class called 'async' and tweaks it to be usable without the rest of the cppcoro library.

class async

async is the class to be returned from coroutines.

it is basically a copy of https://github.com/andreasbuhr/cppcoro's task with two changes:

  • while one can call a coroutine from other coroutines like this

    async<> fun0() {
      co_await fun1();
    }

    one can also call a coroutine from synchronous code

    int fun0() {
      fun1().no_wait();
    }

    with no_wait() preventing the coroutine's state from being deleted along with the task in case it is still running and instead deleted it once it's finished.

  • the coroutine is immediately executed.

class interlock

interlock<KEY, VALUE> is the class to be used to suspend and resume coroutines:

auto value = co_await interlock.suspend(key);

will suspend the execution of the coroutine until

interlock.resume(key, value);

resumes it along with providing a value.

TODO

  • for the full 'javascript' experience, add then() and catch() variants to 'async'
  • try to move the 'return !m_coroutine.done();' from awaitable_base from await_suspend() into await_ready() to improve performance.
  • test exception handling
  • async<T&> not yet included in tests

About

lightweight c++20 coroutine task class

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published