5 ms·
Does modern C++ have a decent story for writing nonblocking concurrent code yet? Something that could compete with async-await in C# or monads in functional pro
by willtim 11y ago
Does modern C++ have a decent story for writing nonblocking concurrent code yet? Something that could compete with async-await in C# or monads in functional programming?
- fsloth 11y agoWell, threads just became a part of the standard with C++11. As to how to use them, there is a decent story, you just have to type it yourself :) https://www.manning.com/books/c-plus-plus-concurrency-in-action https://www.manning.com/books/c-plus-plus-concurrency-in-act...
- Aleman360 11y agoawait is coming, maybe even in C++ 17. Visual Studio already implements it. Way more efficient than the C# implementation too (coroutines) Promises are already there.
- Matthias247 11y agoPromises are not really there. The C++11 version of std::future is missing future::then, which makes it useless for most concurrent tasks. You can launch subtasks on other threads and wait for the result with this future implementation, but you can't asynchronously wait for results of tasks in the same thread. This will also only be fixed in C++17. Boost future already implements more of this, but afaik it's still flagged as experimental, so it could change again. And afaik there were also some pending discussions about how futures should interact with schedulers/executors.
- 72deluxe 11y agoNot really familiar with async-await but perhaps std::future and std::promise are helpful?? Less low-level than threads to some extent.
- acconsta 11y agoA networking library based on Asio has been proposed for C++17. In the meantime, use Asio. http://think-async.com/ http://think-async.com/
- corysama 11y agohttp://think-async.com/Asio/AsioStandalone http://think-async.com/Asio/AsioStandalone !! I've always heard that Asio is great, but never bothered to look because I don't like introducing Boost's 450 megs of compiler-test-suite-worthy C++ to my >45kb programs. But, as a stand-alone library. That's interesting!
- matt_d 11y agoI'd recommend to take a look at Seastar: http://www.seastar-project.org/ http://www.seastar-project.org/ Briefly: "Seastar is event-driven and supports writing non-blocking, asynchronous server code in a straightforward manner that facilitates debugging and reasoning about performance." -- http://www.scylladb.com/2015/02/20/seastar/ http://www.scylladb.com/2015/02/20/seastar/ Concurrency model: "Seastar futures/promises/continuations (f-p-c) are a subset of reactive programming. Seastar performance derives from the sharded, cooperative, non-blocking, micro-task scheduled design, and f-p-c are a friendlier way of feeding tasks to the scheduler. Seastar’s f-p-c do have some optimizations relative to other f-p-c designs. They trade off thread safety, which is unneeded due to the sharded design, for scheduling efficiency, and have a very low memory footprint." -- http://www.seastar-project.org/faq/ http://www.seastar-project.org/faq/ If you'd like to find out more, here are a few links with more information (including examples and tutorials): - https://github.com/scylladb/seastar/wiki https://github.com/scylladb/seastar/wiki - https://github.com/scylladb/seastar/blob/master/doc/tutorial.md https://github.com/scylladb/seastar/blob/master/doc/tutorial... - http://www.seastar-project.org/futures-promises/ http://www.seastar-project.org/futures-promises/ - http://blog.cloudius-systems.com/2015/04/29/seastar-tutorial/ http://blog.cloudius-systems.com/2015/04/29/seastar-tutorial... - http://www.slideshare.net/TzachLivyatan/seastar-sayeret-lambda http://www.slideshare.net/TzachLivyatan/seastar-sayeret-lamb... In terms of practical applications, ScyllaDB (fully compatible--and significantly faster--drop-in replacement of Apache Cassandra) relies on Seastar: http://www.scylladb.com/ http://www.scylladb.com/ ScyllaDB has been discussed here before: https://news.ycombinator.com/item?id=10262719 https://news.ycombinator.com/item?id=10262719