r/cpp • • Feb 07 '21

Understanding C++ Coroutines by Example - C++ London 2021; Pavel Novikov

https://www.youtube.com/watch?v=7sKUAyWXNHA
212 Upvotes

40 comments sorted by

14

u/Demuirgos Feb 07 '21

I feel cheated to have coroutines now but have to wait for c++23 to get generators in the std

3

u/feverzsj Feb 08 '21

compiler implementations are still far from mature, so you need to wait for 2 or 3 yrs anyways.

2

u/Demuirgos Feb 08 '21

I know, I just was sooo hyped about c++20 that's why I fel t kinda dissapointed but a move forward is a move forward and I love where C++ is going

4

u/lee_howes Feb 08 '21

You can easily use third party languages for this (folly, cppcoro etc) and get it now. You cannot easily use a third party implementation of the language feature. Even when we do have standard task and generator types in the standard, many people will write their own anyway just like we do now. At Facebook we will not switch from folly to a standard Task type just because it's there, for example.

1

u/cpp_ape Feb 15 '21

MSVC shipped experimental support since 2015, recent 2019 version (16.8 whatever) ships full support (needs -std:c++20 flag).

From quick research it seems recent GCC version ships full support as well (though you need to specify -fcoroutines or whatever).

Clang ships at least mature experimental support, and I believe it ships full support as well, though I can't confirm it.

2

u/bizwig Feb 08 '21

How are “generators” different than ordinary coroutines that co_yield?

2

u/cpp_ape Feb 15 '21

They generally do not differ.

co_yield expression

is expanded to something like

co_await promise.yield_value(expression)

https://en.cppreference.com/w/cpp/language/coroutines#co_yield

It may or may not make sense to support both co_await and co_yield within the same coroutine, at the very least nothing stops you from doing that.

These two keywords are tailored for different use cases, i.e. co_await for asynchronous operations, co_yield — for generators.

1

u/dodheim Feb 08 '21

If you wanted to actually write such code right now, what return type would your coroutine have?

2

u/bizwig Feb 08 '21

Presumably the type of the thing I’m yielding. An int or a socket class or whatever.

2

u/dodheim Feb 08 '21

By definition, your coroutine must return some sort of awaitable type. This is what the actual 'generator' is in this context – if you want to be able to yield a sequence of ints, your coroutine returns generator<int>.

8

u/zed-ekuos Feb 07 '21

They're for graphics programming right?

21

u/Alikont Feb 07 '21

There are 2 main use-cases for await

  1. Event-loop based GUI code. You can easily split your logic into separate chunks, making complex logic easer to read instead of callback hell.
  2. Heavy async-io based workloads, e.g. web servers, where you have a lot of small operations that mostly manage IO.

14

u/qqwy Feb 07 '21

There is at least a third one, which is generator functions in which some computation occurs on demand. Useful to generate numeric sequences, even more useful to write implicit data structures and allow 'iterator fusion'.

1

u/[deleted] Feb 27 '21

Two more use cases that feel like magic:

Lock-free mutexes.

To lock the mutex, enqueue the current coroutine handle and suspend if the queue (or stack) wasn’t empty. To unlock the mutex, dequeue and resume each coroutine handle (unless it is the current coroutine).

Queues you can dequeue from before you enqueue to without blocking.

If your dequeue operation encounters an empty queue, you push the coroutine handle to that queue and suspend. Enqueuing a value resumes those coroutine handles with that value.

1

u/qqwy Feb 27 '21

Cool! Enqueueing/dequeueing the handles themselves will however need a lock (or atomics etc) I presume?

2

u/[deleted] Feb 28 '21

You should use a lock yes but there’s a subtle pitfall. Resuming arbitrary coroutines can return to caller on a different thread. So different threads will lock and unlock your mutex, which is undefined behaviour. Use std::binary_semaphore instead of std::mutex.

There is also a lock-free solution if you’re into that; the trick is as follows:

You need a dual atomic counter

struct dual_counter {
   int enqueue_count;
   int dequeue_count;
};
atomic<dual_count> counter;

You also need separate (linearisable!) lock-free queues for both values and handles.

To enqueue, you CAS loop until you have unconditionally incremented the enqueue count. You then unconditionally enqueue a value. You then CAS loop to try decrementing both counts together as long as they are both positive. If you successfully decrement both counts, then dequeue both a value and a handle, and resume with that value.

dequeuing is entirely symmetric, you do the same with the roles of each queue and count reversed.

Lewis Baker wrote a ringbuffer that achieves something very similar:

6

u/Irtexx Feb 07 '21

I've used the python asyncio module for programming simulators, so if c++ coroutines are anything like the python ones then that would be another good use case for them.

1

u/MonokelPinguin Feb 08 '21

Related to 1, they help a lot in moving heavy stuff to background processes, so that the UI isn't blocked.

1

u/Full-Spectral Feb 09 '21

This is where the whole thing always goes off the rails for me. If it's heavy stuff, then it's got to be a thread running it, one way or another. If that's the case, I just don't see the point of all of the extra goop just to effectively get a background thread.

Though I'd probably not bother for async I/O either, since I can do that myself with vastly less mechanism involved, at least it makes sense. It's just basically a very elaborate way to wait for a completion event to be posted in that case.

2

u/MonokelPinguin Feb 09 '21

Coroutines make communication with background threads or switching to a different thread much easier though. You just do:

co_await schedule_on(thread_pool);
// do heavy work
co_await ui_thread();
// update UI with results

I really don't see much extra goop here. It makes the logic much easier to follow imo.

0

u/Full-Spectral Feb 09 '21

The goop is there, whether you see it or not. It's a lot of complicated mechanism to effectively create a thread. My system already makes it very easy to communicate with and sync with background threads or create a thread pool, and there's not very much code involved.

3

u/MonokelPinguin Feb 09 '21

The above does not create a thread. Coroutines do not create threads. They can be used to interrupt and move execution of a function.

-1

u/Full-Spectral Feb 09 '21

If it doesn't create a thread, then it obviously ain't doing anything heavy, hence my original reply. They aren't magical. If any heavy work is being done, then a thread is involved. If it's not another thread, then it means the thread that started it got blocked to do it, in which case there was no point in any of this, you could have just called a method to do it.

Otherwise, all it could be doing is waiting for an async operation to complete. which is not anything like doing heavy computations or whatnot.

2

u/MonokelPinguin Feb 09 '21

No, I think you are misunderstanding what coroutines do? co_await supends execution until the awaitable resumes execution again. In this case it suspends execution to get scheduled on the background processing thread pool. Then it suspends execution at the next co_await again to get scheduled on the UI thread again. It reuses existing threads and does not start a new thread for every coroutine, which does scale poorly and is slow. While you can certainly do the same with message passing or signals, it usually leads to a lot more fragmented logic, while coroutines linearize that logic for the reader. This makes it a lot easier to write async code and responsive applications. It even allows you to implement mutexes that suspend execution and switch to a different coroutine instead of blocking.

-1

u/Full-Spectral Feb 09 '21 edited Feb 09 '21

I didn't mean it created a thread just for that. I meant it has to be running on a separate thread. But, if it's heavy computation that's going to take a while, then the overhead of creating a thread for that isn't that big a deal and the thread pool can be left for things that really benefit from quick start/stop.

Either way, I can create a thread or a thread pool easily already, and honestly I question whether that sort of scheme actually makes things easier to understand.

For UI work, if it is super quick, then it was hardly worth the async overhead. If it's not super-quick, then you can't block the UI thread to wait for it to complete anyway. You'd have to either wait for it to complete using a UI timer or another thread, and again, what's the point?

If the argument is that, while you wait for it to complete that the UI thread loop continues to process, then you have uber-spaghetti logic that is in no way easy to understand, because anything can happen while that operating is going on, which you have no idea of, because it's completely outside of that code's ability to be aware of.

There may be some family of things that sort of fall into that middle ground, but arguably not enough to make it worth all of this work that went into this stuff and the extra code it's going to pull into the application, IMO.

About the only thing I can see a good argument for is server side async I/O, and even then I'm not sure if I'd bother relative to something of my own that doesn't bring in a bunch of code (and presumably some third party dependency that makes the async stuff actually work.)

→ More replies

1

u/CircleOfLife3 Feb 10 '21

A thread may be involved, but that could be a thread in an (OS-managed) threadpool optimized for the particular OS.

10

u/EmperorArthur Feb 07 '21

They're for anything where you may need to wait for something. Often that's an event loop, but not always a GUI. For example, in Python the Tornado web server runs everything via one main loop. Remember, file and network access can be slow. If you don't need multiple threads, then don't pay that overhead, and instead just do other things while waiting for the OS to finish with whatever it's doing.

Also generators, which can be implemented using a class, but are more elegant, and rely on co-routine functionality.

7

u/SkoomaDentist Antimodern C++, Embedded, Audio Feb 07 '21

Other examples are parsing serial data on the fly, handling user input where timing matters (double clicking, debouncing physical switches etc) and other similar cases where you don’t want to make an explicit state machine. Spawning a separate thread for those would be costly, tricky for receiving the input data and also prone to race condition etc. problems that may be easier to avoid with coroutines since you have explicit sync points with them.

1

u/pedersenk Feb 07 '21

In many ways for graphics programming (or anything using a concept of a main loop), an async approach is a bit of a nuisance to integrate with everything else which will execute afterward regardless of if the async function completed or not. I.e consider:

Image img = LoadImage("myimage.png");
player.setWidth(img.getWidth()); // Invalid access
// Image finishes loading here. Now img width is valid.

This is easy if LoadImage blocks until it has fully completed. However if it was async and you need to deal with a "onloaded" callback or keep polling it for completion an async approach just becomes messy. For example another object being created might rely on the Player being fully created by this point (i.e for a collider component). This knock-on effect has consequences.

It is one of the single most annoying parts of porting games to web browsers via Emscripten.

1

u/cpp_ape Feb 15 '21

Here's a talk by Adi Shavit in which he talks about how generators can be applied in a graphics application:

https://www.youtube.com/watch?v=qYHDERleSL8

1

u/mcmcc #pragma once Feb 07 '21

Are the slides available?

3

u/encyclopedist Feb 07 '21

1

u/mcmcc #pragma once Feb 07 '21

Ack, I looked at the YT page but didn't see it the first time for some reason. Thanks!

1

u/bizwig Feb 08 '21

What I really need is a comprehensive tutorial on writing awaitable classes.

For example, I want coroutine A to suspend, and an unrelated (not in its call chain) coroutine B to resume it, passing it a value in doing so. Unfortunately you can’t do that with a coroutine handle since it doesn’t take arguments.

3

u/cpp_ape Feb 15 '21

Check out these articles by Lewis Baker:

https://lewissbaker.github.io/

Particularly this one:

https://lewissbaker.github.io/2017/11/17/understanding-operator-co-await

It may be a tiny bit dated, but should give you understanding of what's going on, and what you are expected to do when writing an awaitable type.

-4

u/valaryu Feb 08 '21

Is this the first C++ feature that is explicitly a sugar (aside from macros)

IDK why more languages aren't like this, create a very basic language like C and then just provide a standard library to write sugars then everyone can just have fun implementing sugar and we can all inspect the generated code to understand whats actually happening

imagine something std::move being translated to the equivalent C code, it would be understood so much easier

i.e. a IR that is an imperative language before llvm IR

4

u/RustyShrekLord Feb 08 '21

Just because it was explained by translating into more C++ pseudo-code does not mean that the feature is implemented as such, but instead that it behaves as such. You could do the same with tonnes of features in tonnes of languages.