Recommended way of doing concurrency with Boost
Hi all, I'm planning to start working on a new Web3 project and I'm trying to decide how I should approach doing concurrency with C++ and Boost. It seems like using callback-passing style with Asio has been pretty popular historically. However, having worked with other modern languages with first-class async support, this feels a bit primitive. I've heard about C++20 coroutines and it seems like Boost has a library for it (Cobalt). However, it's relatively new, so I'm curious if anyone had experience with it and is it mature for production. I see that Asio also has coroutine support, so what's the difference? The third option I'm considering is using fibers. I like this option in principle because the resulting code is very clean and looks like normal synchronous code. However, I haven't seen many big examples/projects using Boost.Fiber with Boost.Asio. Is this something people are doing out in the wild? Does Asio support Fibers well? Best regards, Thomas
You could use boost::asio::spawn() (utilizes stackful coroutines). Am Di., 13. Aug. 2024 um 12:20 Uhr schrieb Thomas Fowlery via Boost < boost@lists.boost.org>:
Hi all,
I'm planning to start working on a new Web3 project and I'm trying to decide how I should approach doing concurrency with C++ and Boost.
It seems like using callback-passing style with Asio has been pretty popular historically. However, having worked with other modern languages with first-class async support, this feels a bit primitive.
I've heard about C++20 coroutines and it seems like Boost has a library for it (Cobalt). However, it's relatively new, so I'm curious if anyone had experience with it and is it mature for production. I see that Asio also has coroutine support, so what's the difference?
The third option I'm considering is using fibers. I like this option in principle because the resulting code is very clean and looks like normal synchronous code. However, I haven't seen many big examples/projects using Boost.Fiber with Boost.Asio. Is this something people are doing out in the wild? Does Asio support Fibers well?
Best regards, Thomas
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
On Tue, Aug 13, 2024 at 3:32 PM olk via Boost
You could use boost::asio::spawn() (utilizes stackful coroutines).
Hmm, thanks. My concern with this is having to pass the boost::asio::yield_context parameter everywhere. I suppose I could use a thread_local to pass the parameter implicitly somehow, although I'm not sure how to do it safely in this context.
Am 13.08.24 um 16:49 schrieb Thomas Fowlery via Boost:
On Tue, Aug 13, 2024 at 3:32 PM olk via Boost
wrote: You could use boost::asio::spawn() (utilizes stackful coroutines).
Hmm, thanks. My concern with this is having to pass the boost::asio::yield_context parameter everywhere. I suppose I could use a thread_local to pass the parameter implicitly somehow, although I'm not sure how to do it safely in this context.
Its simple: |int main () {||||boost::asio::io_service io_service;||boost::asio::spawn( io_service, [io_service](||boost::asio::yield_context yc)||{ for (;;) { ||boost::asio::deadline_timer timer(io_service); timer.expires_from_now(boost::posix_time::seconds(1)); std::cout << "do wait" << std::endl; timer.async_wait(yc); std::cout << "woken up" << std::endl; | |} }); io_service.run(); } |
Thomas Fowlery via Boost
hat am 13.08.2024 11:36 CEST geschrieben: It seems like using callback-passing style with Asio has been pretty popular historically. However, having worked with other modern languages with first-class async support, this feels a bit primitive.
The awaitables that come with Asio work beautifully with everything that can be scheduled onto Asio work queues. We do this here with great success at the company, and I gave a talk at CppCon 2022 showing a small application (network, ffmpeg, and sdl) how you can do that, too. Thanks Dani
Hi Daniela, I just watched your 2022 cppcon talk. It was mind blowing! Thanks! I couldn't find the slides on github.com/cppcon/cppcon2022. Is it possible to get them uploaded? Thanks Georg
I guess asio is the answer, but I agree I wish there were better libs in boost for that. - Soda
On Wed, Aug 14, 2024, 13:57 [soda-pressed] via Boost
I guess asio is the answer, but I agree I wish there were better libs in boost for that.
Could you define "better"? Regards, &rzej;
- Soda
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Could you define "better"?
I feel there's not a lot of modernisation in asio these days. I wish we had better models of how to build concurrent applications, and better libraries that solve the runtime issue in creative ways. I can't imagine using asio for some complex, UI event driven, type of application. Asio is great, but it is too barebones, and I think there should have been some advancements at this point in this area. - Soda
Hello Daniela,
I found it in your 2023 talk.
https://github.com/DanielaE/CppInAction.git
Bye
Georg
14.08.2024 07:09:37 Georg Gast
Hi Daniela, I just watched your 2022 cppcon talk. It was mind blowing! Thanks!
I couldn't find the slides on github.com/cppcon/cppcon2022. Is it possible to get them uploaded?
Thanks Georg
On Tue, Aug 13, 2024 at 3:20 AM Thomas Fowlery via Boost < boost@lists.boost.org> wrote:
Hi all,
I'm planning to start working on a new Web3 project and I'm trying to decide how I should approach doing concurrency with C++ and Boost.
I think it'd help us help you the most if you talked a little bit about what your requirements are and what your goals are as well. There's many different solutions to concurrency in C++. Asio is obviously I/O focused. It does also contain a simple thread pool implementation. But there's also other concurrency libs like TBB, which are more parallel-focused. In principle, I don't think there's anything inherently incorrect about using the Asio thread pool as a generic task runner. - Christian
The third option I'm considering is using fibers. I like this option in principle because the resulting code is very clean and looks like normal synchronous code. However, I haven't seen many big examples/projects using Boost.Fiber with Boost.Asio. Is this something people are doing out in the wild? Does Asio support Fibers well?
Here's a complete web server that uses Asio, Beast, Redis, MySQL and asio::spawn. https://github.com/anarthal/servertech-chat/ I'm considering migrating this to C++20 coroutines. Regards, Ruben.
On Wed, Aug 14, 2024 at 8:30 PM Ruben Perez
The third option I'm considering is using fibers. I like this option in principle because the resulting code is very clean and looks like normal synchronous code. However, I haven't seen many big examples/projects using Boost.Fiber with Boost.Asio. Is this something people are doing out in the wild? Does Asio support Fibers well?
Here's a complete web server that uses Asio, Beast, Redis, MySQL and asio::spawn.
https://github.com/anarthal/servertech-chat/
I'm considering migrating this to C++20 coroutines.
Regards, Ruben.
Thanks, I'll check this out.
participants (9)
-
[soda-pressed]
-
Andrzej Krzemienski
-
Christian Mazakas
-
Daniela Engert
-
Georg Gast
-
oliver.kowalke@gmail.com
-
olk
-
Ruben Perez
-
Thomas Fowlery