当我执行下面的代码时,shell 输出可能会在线程 0 和线程 1 之间重叠。所以我想问一下,确保每个线程在另一个线程开始输出之前完成输出写入的最佳方法是什么?
这将确保输出是干净的。
提前谢谢了!
#pragma omp parallel num_threads(2) shared(emperor)
{
#pragma omp sections
{
#pragma omp section
{
cout << "Open the listening thread by using openmp!" << endl;
emperor->startServerListening(); /// does some work
}
#pragma omp section
{
cout << "Open the master thread by using openmp!" << endl;
emperor->startServerCoupling(); /// does some other work
}
} /// End of sections
} /// End of parallel section