-
Notifications
You must be signed in to change notification settings - Fork 4
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
Comments
If you're using CO2, there's |
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;
} |
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. |
But on a second thought it's doable https://github.com/cos-public/coro-task/blob/master/include/task.h |
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. |
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. |
Is there any task<T> implementation which makes the example possible (with VC++ 2017 /await)?
The text was updated successfully, but these errors were encountered: