19 ms·
C++ Coroutines
- zabzonk 4y agoperhaps the take here is that many features of modern c++ are not intended for application writers, but more for library writers?
- ghostwriter 4y agoThey serve both to a great extent, as long as code owners are willing to refactor their codebases. The changes do actually make code significantly safer and more pleasant to work with. My favourite ones are the concepts and class template argument deductions.
- corysama 4y agoThat's definitely a thing. A whole lot of the features added since C++11 have been specifically intended to enable library writers to do very complex things under the hood so they can present powerful yet simple interfaces to users.
- mrfox321 4y agoUsers of coroutine-friendly libraries will have to adopt the async function coloring (co_await, co_yield, co_return, Task<T>). So saying that coroutines are low-level / not application code is misguided.
- nly 4y agoNot necessarily. Coroutines are invisible at the ABI level (that is coroutines are just functions). It's pretty easy to write a library with them (using your own executor) and not reveal their use to the user. Obviously sharing an executor between application and library is another matter.
- Joker_vD 4y ago> sharing an executor between application and library is another matter. Especially when there is not one, but several libraries, and the application itself doesn't actually has or uses any executors, now that's a fun challenge.
- mrfox321 4y agoGood point. Although doesn't that defeat the purpose of structured concurrency? i.e. creating tasks that are plumbed to some background thread/executor. By directly invoking the coroutine, lifetime issues become much easier to avoid.
- steveklabnik 4y agoI can't speak to modern C++ specifically, but in many, many languages, more complex features are often for library writers than application writers.
- civicsquid 4y agoI believe this is correct. Anecdotally, at FB, there was quite a push to get coroutine-supporting-features into the standard because we had a C++ coroutine library built through non-standard means, which meant that compilers didn't fully support them (leading to a bunch of annoying things for all of those writing code on top of the library). Now the folly/experimental/coro library can be compiled by anything that claims to support C++20, which is a pretty big win for FB if nothing else.
- danuker 4y agoWhy implement coroutines when you have multithreading? Coroutines are to me a brain-twisty way to make the most out of a single core, because your language doesn't support multithreading. For example: JavaScript with a single thread in the browser, Python with the GIL. C++ has threads which ACTUALLY run in parallel on the CPU. Why bother complicating the language further?
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- steveklabnik 4y agoWhy use a hammer when you already have screwdrivers?
- com2kid 4y ago> Why implement coroutines when you have multithreading? Because coroutines are easier to think about than threads, and avoid entire (large) classes of bugs. Because coroutines (typically) have a lower overhead than threads. Because if you are IO bound and not CPU bound, and therefore you don't need to spread work across cores, threads may not provide a given workload with much, or any, benefit.
- sgtnoodle 4y agoI've benefited from cooperative userspace context switching with CPU bound workloads as well. Imaging simulating a vehicle's worth of embedded software, with several dozen tasks originally intended to run at 100Hz asynchronously across multiple different computers. Now, imagine running them faster than real time, deterministically. Now imagine doing several thousand of those simulations. The overhead of OS threads, context switching and synchronization starts to add up, and is 10-100x slower for that sort of workload.
- com2kid 4y ago
- artemonster 4y agoThe fact that they went for stackless is a testament to how bad committee-driven design process is. just a bunch of bureaucrats playing important actors, similarly to stackoverflow mods.
- colejohnson66 4y agoCan you ELI5 why stackless is bad?
- artemonster 4y agoFor coroutines to be really useful, they have to be stackful. The guy that originally did the proposal for C++ is also an author of multiple coro libraries and did research on this. You can emulate stackful coros with stackless via trampoline, but heck, you can emulate stackless coros with closures and trampoline too! The requirement of this extra trampoline makes their use extra convoluted and negates the very slight advantages that such functionality may really bring. Making stackful "right" was hard, so a useless compromise was made, which is basically like adding a syntactic sugar to the language.
- int_19h 4y agoIt may be a compromise, but what exactly makes it "useless"? A similar style of async has been in use in e.g. C# for over a decade now, and it has been very successful there. Yes, it is syntactic sugar - but so are e.g. lambdas. What matters in practice is that it makes some kinds of code much shorter and easier to read.
- artemonster 4y agostackful coroutines is a feature that is on par with power of delimited continuations (proven fact in academia, you can easily find somewhat easy to follow papers on this topic), stackless coroutines is a stupid gimmick. You see the "compromise"? You have argued to have a case to add a car, but after long debate, politics and "compomsie" you got a TOY car instead.
- malkia 4y agoI'm only sitting here, waiting for my thread_locals to get broken.... or maybe they are handled magically somehow
- jcelerier 4y ago.. why would they be broken ? coroutines are not related to threads
- grogers 4y agoI think the point is that if the coroutine suspends it could be resumed on a different thread. So you don't really want to mix thread locals and coroutines.
- jcelerier 4y agoI mean, for sure but that's not a property of coroutines, any callback system can do that e.g. if you have this code with boost: #include <boost/asio.hpp> #include <chrono> #include <vector> #include <thread> #include <iostream> std::mutex print_mutex; int main() { using namespace boost::asio; using namespace std::literals; io_context io_context(20); auto work_guard = make_work_guard(io_context); for(int i = 0; i < 100; i++) { post(io_context, [&] { thread_local int x = 0; int z = x++; std::lock_guard _{print_mutex}; std::cout << z << "\n"; }); } std::vector<std::jthread> v; for(int i = 0; i < 20; i++) v.emplace_back([&] { io_context.run_for(2s); }); } i hope you don't expect the output to be "0, 1, 2, ..., 99"
- deleted 4y ago[deleted]
- malkia 4y agostate, e.g. assuming that there is single flow of operations, and nothing gets swapped out (logically). This breaks with co-operative scheduling systems - e.g. task thread while waiting on I/O (for example) gets reused for some other task, hence they get to see the same thread_local, and somewhere someone assumed "thread_local" == "task_local" of sorts (but not).
- jmull 4y ago> Asynchronous code (e.g. callbacks) is more efficient (letting you do other work while waiting for things) but is also more complicated (manually saving and restoring state). This is a persistent misunderstanding about async... it is not generally more efficient than non-async code. In fact, the overhead of async may slow things down. (There can be other downsides as well. E.g. debugging may be more difficult.) For it to help, you need to have something better for the current thread to be doing than waiting for whatever it is you're waiting for. Whatever that is, another worker thread could be doing it, so what we're really talking about is that async may let you reduce the number of threads you're using. (Async is so useful in JS because it lets you reduce the number of threads to one, which lets you sidestep all kinds of hairy issues.) That might be good enough, depending on your situation, to overcome the downsides of async worthwhile. But it may very well not. Anyway: don't use async because it's "more efficient".
- KMag 4y agoAgreed in general, that async code is generally less efficient than epoll/kqueue/poll/select/etc. non-blocking I/O. However, note that async code is often quite a bit more efficient than naive Java 1.3-style (pre-NIO) tons-of-blocked-threads code. Now, io_uring async code is yet another story. I was working at LimeWire when Apple finally shipped a 1.4 JVM as their system JVM. The whole thing got much lighter weight when we could use non-blocking I/O.
- junon 4y agoEh? Async code almost always uses the APIs you describe.
- KMag 4y agoThe async runtime is almost always built on top of non-blocking I/O APIs, and the extra layer of abstraction adds overhead. You're rarely calling epoll in your async code, but rather your runtime is handling all of that for you. That is, unless you're using io_uring, POSIX AIO, etc., in which case your async code might not have the extra layer of abstraction on top of the system APIs.
- Night_Thastus 4y agoEDIT: I think I follow how this works now. I didn't understand the sieve itself, so the co-routines was throwing me for a loop. We get all the numbers from 2 to 40, print out 2, then eliminate all the entries divisible by 2. Now we're left with 3, 5, 7, 9, etc. We print out 3, then remove all the entries divisible by 3. This repeats until we've hit the end (40). The next number after a round of eliminations will always be a prime.
- layer8 4y agoYeah, the sieve use-case is a bit too clever IMO to be a good introductory example.
- convery 4y agoTBH, everytime I read about coroutines and their example usages I get stuck on the "why" question. If the example is a function waiting for data, a std::future seems a lot easier. If the example is a generator, a range/transform/filter or just a class that returns a std::default_sentinel iterator seems more appropriate. The only reason I can see is lowering the overhead of creating threads, at the cost of harder to read controlflow.
- csb6 4y agoOne reason is to reduce callback hell where you have to pass a continuation function to an asynchronous operation to run after it has completed. Coroutines can be used to make your code look more like blocking code. ASIO’s coroutine support does this and it makes async operations much easier to read Also, generators can be used to turn traversal functions into iterators, which is often tedious to do by hand.
- blt 4y agoC++20 coroutines didn't include the facilities to get a coroutine directly from writing a loop with the `yield` keyword like in Python. You can see all the boilerplate needed in this post's `Generator` class. For me, it made the feature not worth using. I'm excited that C++23 is getting `std::generator`, so we can finally write those simple loops: https://en.cppreference.com/w/cpp/coroutine/generator https://en.cppreference.com/w/cpp/coroutine/generator
- andrewstuart 4y agoMy understanding is C++ coroutines are implemented as threads, which seems to take away the point of coroutines.
- MathMonkeyMan 4y agoI haven't read much about them, but it looks like they can be driven by any event-loopy thing. It's not like creating a coroutine creates a thread. https://en.cppreference.com/w/cpp/language/coroutines https://en.cppreference.com/w/cpp/language/coroutines
- jcelerier 4y agowhere did you get this understanding? this is definitely, 100% wrong, C++ coroutines are not backed by threads at all (or by anything really since as of C++20, the language only provides the building blocks for making your own coroutine / generator types and not actual implementations).
- andrewstuart 4y agoI got that understanding here: https://en.cppreference.com/w/cpp/thread/async https://en.cppreference.com/w/cpp/thread/async
- jcelerier 4y agocoroutines (https://en.cppreference.com/w/cpp/language/coroutines https://en.cppreference.com/w/cpp/language/coroutines) are unrelated to std::async
- andrewstuart 4y agoIn other languages they are tightly related to async/await and I made the mistake of assuming they were connected.
- srott 4y ago
- PointyFluff 4y ago[dead]
- samsquire 4y agoI recently asked how to use C++ thread pools with coroutines. there is code as an answer on my stack overflow question. https://stackoverflow.com/questions/74520133/how-can-i-pass-things-to-a-c20-coroutine-co-await-await-suspend-operator-funct https://stackoverflow.com/questions/74520133/how-can-i-pass-... I recently ported the coroutine code from https://blog.dziban.net/coroutines/ https://blog.dziban.net/coroutines/ To GNU Assembler. https://GitHub.com/samsquire/assembly https://GitHub.com/samsquire/assembly see coroutines.S I think the best combination is threads + coroutines with a IO event loop. This gives throughout for CPU tasks and IO scalability.
- throwaway81523 4y agoArticle is pretty good but C++ coroutines sound like a contorted version of protothreads. Not that useful in the first place, and made way more painful for the sake of enabling some hypothetical compiler optimizations that aren't done in practice. The two specific pain points are 1) stacklessness, which means coroutines can only yield from the top-level function that is called by the coroutine, sort of like the "simple generators" introduced in Python 2. Some people would consider these to not be real coroutines at all, and Python 3 later introduced actual (stackful) coroutines aka async functions. The C++ devs say that if their coroutines had stacks they would be called fibers instead of coroutines, but I had always thought fiber (used that way) was a Windows-specific term. 2. Opaqueness of the coroutine frame object: you can't get at its size or contents. That lets the compiler optimize its layout or even in some cases eliminate it. But at least for now, real compilers don't seem able to do that, and s0me LLVM devs aren't sure it's even feasible. Rust doesn't attempt this. It just uses an ordinary struct to hold the frame data, so you can do normal struct things with it. Above is my impression from reading the linked article and some related ones. I have not yet tried to use C++ coroutines, or looked into Rust's even slightly. Protothreads (a C header library) are cool though. They don't do that much, but they are very simple in implementation compared to this C++ mess. I think these C++ coroutines don't reach the usefulness of a lightweight cooperative Forth multitasker from 50 years ago. Those were stackful (typically a few dozen cells in each stack, so a task cost on a 16-bit cpu might have cost 200 or so bytes including stacks, user variables, and other data) and ridiculously simple. Goroutines aren't comparable. They are way more powerful but also way more expensive than C++ coroutines. There were apparently some other C++ coroutine proposals that sounded a lot better, but that didn't have the backing to get through the standardization process. I don't know how Boost coroutines compare. I tried to use them once but didn't get them working.