这是由于cout的缓存造成了,有两种办法
第一、强制缓存输出
std::cout << "test" << std::flush; // std::flush is in <iostream>第二种、不缓存
或
std::cout << "test";
std::cout.flush(); // explicitly flush here
或
std::cout << "test" << std::endl; // endl sends newline char(s) and then flushes
std::setvbuf(stdout, NULL, _IONBF, 0);或运行c++程序的时候加入以下参数
stdbuf -o 0 ./yourprogram --yourargs
网友回复