Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task<T> implementation #3

Closed
cos-public opened this issue Aug 9, 2018 · 6 comments
Closed

task<T> implementation #3

cos-public opened this issue Aug 9, 2018 · 6 comments

Comments

@cos-public
Copy link

cos-public commented Aug 9, 2018

Is there any task<T> implementation which makes the example possible (with VC++ 2017 /await)?

@jamboree
Copy link
Owner

If you're using CO2, there's co2::task<T>. If you're using Coroutine TS, you can try cppcoro, which provides cppcoro::task<T>, but it's more like CO2's lazy_task (i.e. only starts the task when you await on it). I haven't tried cppcoro myself though.

@jamboree
Copy link
Owner

If you just want to try the example, below is enough:

#include <iostream>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/write.hpp>
#include <act/socket.hpp>
#include <act/write.hpp>
#include <act/acceptor.hpp>
#include <experimental/coroutine>

namespace asio = boost::asio;

namespace std::experimental
{
    template<class... T>
    struct coroutine_traits<void, T...>
    {
        struct promise_type
        {
            void get_return_object() {}

            suspend_never initial_suspend() noexcept
            {
                return {};
            }

            suspend_never final_suspend() noexcept
            {
                return {};
            }

            void return_void() noexcept {}
        };
    };
}

void session(asio::ip::tcp::socket sock)
{
    try
    {
        char buf[1024];
        std::cout << "connected: " << sock.remote_endpoint() << std::endl;
        for (; ; )
        {
            act::error_code ec;
            auto len = co_await act::read_some(sock, asio::buffer(buf), ec);
            if (ec == asio::error::eof)
                co_return;
            co_await act::write(sock, asio::buffer(buf, len));
        }
    }
    catch (std::exception& e)
    {
        std::cout << "error: " << sock.remote_endpoint() << ": " << e.what() << std::endl;
    }
}

void server(asio::io_context& io, unsigned short port)
{
    asio::ip::tcp::endpoint endpoint{asio::ip::tcp::v4(), port};
    asio::ip::tcp::acceptor acceptor{io, endpoint};
    asio::ip::tcp::socket sock{io};
    std::cout << "server running at: " << endpoint << std::endl;
    for (; ; )
    {
        co_await act::accept(acceptor, sock);
        session(std::move(sock));
    }
}

int main(int argc, char *argv[])
{
    asio::io_context io;
    server(io, std::atoi(argv[1]));
    io.run();

    return EXIT_SUCCESS;
}

@cos-public
Copy link
Author

Yes, I'm aware of cppcoro task, but as you said it has lazy evaluation. I'm struggling to find a preferably cross-platform task implementation with eager evaluation, nested coroutines and exception support.
I've tried to implement one, but it seems that eager evaluation and nested coroutines are mutually exclusive.

@cos-public
Copy link
Author

cos-public commented Aug 10, 2018

But on a second thought it's doable https://github.com/cos-public/coro-task/blob/master/include/task.h

@jamboree
Copy link
Owner

Beware of data race. While one thread awaits on the task (i.e. await_suspend is called), in the meantime another thread may have the task completed (i.e final_suspend is called). Lazy task avoids that problem by not starting eagerly.

@jamboree
Copy link
Owner

Just a note, I've ported some stuff from CO2 to CoroutineTS, you can check it out here. It's trickier than I thought because of some deficiency in CoroutineTS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants