加速异步从标准输入中读取asio

我想使用boost asio函数async_read()从标准输入中读取(就c ++而言,std :: cin)。以下代码无法编译,由于实例化错误,给了我很长的错误消息。有人可以回答,如何使用此功能异步读取stdin?

char buf[64];

boost::asio::async_read(std::cin,boost::asio::buffer(buf),[](const boost::system::error_code &ec,std::size_t size){});

编辑:我知道这里有一个类似的问题Using boost::asio::async_read with stdin?,但是我不清楚标记答案与问题的联系方式

geniusyoi 回答:加速异步从标准输入中读取asio

您必须使用posix::stream_descriptor

例如:

using namespace boost::asio;
io_context ioc;        
posix::stream_descriptor input_stream(ioc,STDIN_FILENO);
// assume that you already defined your read_handler ...
async_read(input_stream,buffer(buf),read_handler);
ioc.run();

请注意,您不应将以上代码与std::cin

混合使用

有关更多信息,请参见here

本文链接:https://www.f2er.com/3131172.html

大家都在问