当 boost 的 io 服务正在运行时,无法执行字符串

Cannot cout string when boost's io service is running

本文关键字:执行 字符串 运行时 boost io 服务      更新时间:2023-10-16

我有一些简单的代码:

#include <iostream>
#include <string>
int main() {
    boost::asio::io_service ioservice;
    TCPServer server(ioservice);
    std::cout << "hello world";
    ioservice.run();
}

我计算上面的hello world字符串。虽然字符串没有被输出到我的终端,当我调用service.run()。当我删除最后一个调用表达式时,可以看到一个输出。令我烦恼的是,我在调用之前进行计数,但没有任何输出。而且,即使在我的TCP服务器上定义的处理程序上,cout似乎也不可用。那么人们在使用Boost Asio时如何登录stdout呢?

您在进入.run()之前没有刷新流,因此您的信息仍然位于缓冲区中。

使用std::cout << std::flush(或std::cout << std::endl也包括换行符)