异步提升从stdin读取的asio

Boost asio read from stdin asynchronously

本文关键字:读取 asio stdin 异步      更新时间:2023-10-16

我想用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){});

编辑:我知道这里已经提出了类似的问题。使用boost::asio::async_read和stdin?,然而,我不清楚标记的答案与问题的关系

您必须使用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混合使用

有关更多信息,请参阅此处。