3 ms·
these days you have std::async(promise+future via channel), std::thread(e.g. pthread style) and coroutines in modern C++. my understanding is std::async is a si
by synergy20 4y ago
these days you have std::async(promise+future via channel), std::thread(e.g. pthread style) and coroutines in modern C++. my understanding is std::async is a simpler API for parallel coding, thread for finer grained control, while coroutine is running on one core|cpu by design thus it needs to work with threadpool(i.e. using std::thread) or something else to utilize all cores, unlike std::async or std::thread, both are multi-core ready.
- interroboink 4y agoDon't forget the upcoming "executors" concept. Good talk w/Eric Niebler on why promise/future is not so great[1] and P0443R14[2]. Seems like it won't make it for C++23, though. [1] https://www.youtube.com/watch?v=tF-Nz4aRWAM&t=778s [2] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0443r14.html
- synergy20 4y agoGood to know. std::async for simple and less intensive cases, for anything serious std::thread might be the way to go, before executors is real that is.
- candiodari 4y agoExcept you can have thousands of std::async threads, and at best a few dozen std::threads.
- inetknght 4y ago> Seems like it won't make it for C++23, though. Why is that?
- interroboink 4y agoI'm not involved, but it seems like they haven't managed to get it fully baked in time for the cutoff. Some more discussion at: https://old.reddit.com/r/cpp/comments/sshm7n/did_unified_executors_make_it_to_c23/ https://old.reddit.com/r/cpp/comments/sshm7n/did_unified_exe...
- tialaramex 4y agoI'm assuming you're wondering about process rather than why specifically this piece of work was not prioritised enough to get done (which you'd have to ask the relevant people about) C++ is a JTC1 standard, which means it needs to use ISO processes, which means it needs to produce a CD (Committee Draft) and then that needs to go through formal international processes in which, in principle, there might be objections from ISO's members (which remember are national standards agencies from around the world, on behalf of the sovereign entities) and then those get addressed and only then, months later, is it published as a standard. So in practice that means WG21 (the C++ committee) needs to have more or less final text for C++ 23 by July 2022. ie the C++ 23 standard is in effect already decided.
- jcelerier 4y agoCoroutines are a tool for concurrency, threads a tool for parallelism - see e.g. https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism https://stackoverflow.com/questions/1050222/what-is-the-diff... . In my new code coroutines have entirely replaced futures, they make the code much more readable. They can also be used for use cases where parallelism is irrelevant, for instance for generators. Think of coroutines as a language-level way to turn procedural-looking call trees into state machines where the caller can control when and how the transitions of the state machine happen.
- bronxbomber92 4y agoThreads are also a tool for concurrency.
- titzer 4y agoAnd concurrency can be a tool to implement threads (albeit, with no parallel speedup).
- synergy20 4y agoconcurrency is a bit overused term, in the early stage it was referring to parallelism too, anyways yes I know the difference between pthread vs coroutines. the problem of coroutine with me is how to use it on multiple core systems, my current thought is to have a pthread-pool in the # of cores, each thread can run multiple coroutines, as again, coroutine seems not an ideal fit to leverage multiple cores. The pthread-pool + coroutine approach is a combination with simpler code and multicore usage to me.