Coroutine 101 - A simple serializer Jul 27, 2023 This is the first post in a series explaining C++20 coroutines by example. Goal The goal for this article is to create a simple non-recursive stream serializer, that can be used like this: stream::serializer serialize_ints(std::vector<int> data) { for (auto idx = 0u; idx < data.size(); idx++) { if (idx != 0) co_yield ','; co_yield std::to_string(data[idx]); } } int main(int argc, char *argv[]) { auto s = serialize_ints({1,2,3,4,5,6,7,8,9,10}); std::string buffer; buffer.resize(10); using std::operator""sv; assert(s. ...
asio - timeouts, cancellation & custom tokens Jan 02, 2023 Since asio added and beast implemented per-operation cancellation, the way timeouts can be implemented in asio code has changed significantly. In this article, we’ll go from simple timeouts to building our own timeout completion token helper. Cancellation A timeout is a defined interval after which a cancellation will be triggered, if an action didn’t complete by then. Timeouts can be a way of handling runtime errors, but one should generally be prudent about their usage. ...
asio - deferred Dec 11, 2022 Asio deferred Aysnc operations Asio introduced the concept of an async_operation, which describes a primary expression that can be invoked with a completion token. In C++20 this is also a language concept. asio::io_context ctx; asio::async_operation auto post_op = [&](auto && token){return asio::post(ctx, std::move(token));}; auto f = post_op(asio::use_future); ctx.run(); f.get(); // void Async operations can be used in parallel_group and directly co_awaited in C++20. asio::deferred as a completion token Using asio::deferred as a completion token, will give you a lazy async_operation as the result value. ...