Hacker News new | ask | show | jobs
by nly 3151 days ago
> I've done lots of event loops in the past (eg hellepoll in c++) and think that the cost of that is on the programmer - keeping track of things, callbacks, state machines and things and avoiding using the stack for state etc is all hard work and easy to mess up.

This is improving, even in C++. This is what the core loop of a line-based echo server could look like in C++17 (and something very similar compiles today on my machine)

    void echo_loop (tcp::socket socket) {
        io::streambuf buffer;
        std::string line;
        std::error_code ec;
        do {
            ec = co_await async_read_line (socket, buffer, line);
            if (ec)
                break;
            ec = co_await async_write (socket, line);
        } while (!ec);
    }
1 comments

Wow. That looks really simple.
Unfortunately it's just exposition, but here[0] is a version that works with Clang 5 + Boost

Echo specific code starts on line 167. Everything above will hopefully be provided by the standard library once both the Networking TS and Coroutine TS merge in to C++20.

One nice thing about lines 1 - 165 though, is that it demonstrates how easy it is to extend the native coroutine capabilities in C++ to support arbitrary async libraries, even if the author of those libraries didn't know anything about coroutines. All this happens without breaking the ability to call these coroutines from C. You can even use async C libraries that only provide a void* argument to your callback.

[0] https://gist.github.com/anonymous/d9a258136431a352516122d1c9...