我正在尝试一些例子,C++11 threads
我看到了一些令人惊讶的结果。使用以下代码
#include <iostream>
#include <thread>
void hello() {
std::cout << "Hello concurrent world " << std::endl;
}
void do_something() {
std::cout << "[background_task] --> [ do_something ]" << std::endl;
}
void do_something_else() {
std::cout << "[background_task] --> [ do_something_else ]" << std::endl;
}
class background_task {
public:
void operator()() const {
do_something();
do_something_else();
}
};
int main ( int argc, char **argv) {
std::thread t(hello);
background_task bt;
std::thread fn_obj_th(bt);
t.join();
fn_obj_th.join();
}
输出如下
Hello concurrent world [background_task] --> [ do_something ]
[background_task] --> [ do_something_else ]
Press any key to continue . . .
如果我替换
std::cout << "Hello concurrent world " << std::endl;
为
std::cout << "Hello concurrent world \n";
结果是
Hello concurrent world
[background_task] --> [ do_something ]
[background_task] --> [ do_something_else ]
为什么std::endl
我没有得到预期的输出。