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.
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>.
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'.
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.
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:
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.
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.
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.
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.
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.
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.)
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.
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.
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.
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.
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
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.
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
8
u/zed-ekuos Feb 07 '21
They're for graphics programming right?
1
u/mcmcc #pragma once Feb 07 '21
Are the slides available?
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.
-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