/* nowait用来取消栅障 */ void test12() { #pragma omp parallel { #pragma omp for nowait for (int i = 0; i < 100; ++i) { std::cout << i << "+" << std::endl; } #pragma omp for for (int j = 0; j < 100; ++j) { std::cout << j << "-" << std::endl; } } }
/* 显式同步栅障 在barrier处进行了同步,然后执行后边的for循环。 */ void test13() { #pragma omp parallel { for (int i = 0; i < 100; ++i) { cout<<"ID "<<omp_get_thread_num()<<" "; std::cout << i << "+" << std::endl; } #pragma omp barrier for (int j = 0; j < 100; ++j) { cout<<"ID "<<omp_get_thread_num()<<" "; std::cout << j << "-" << std::endl; } } }
/* 声明对应的并行程序块只由主线程完成 */ void test14() { #pragma omp parallel { #pragma omp master { for (int j = 0; j < 10; ++j) { std::cout << j << "-" << std::endl; } } std::cout << "This will printed twice." << std::endl; } }
/* section用来指定不同的线程执行不同的部分 */ void test15() { #pragma omp parallel sections //声明该并行区域分为若干个section,section之间的运行顺序为并行的关系 { #pragma omp section //第一个section,由某个线程单独完成 for (int i = 0; i < 5; ++i) { std::cout <<"i "<< i << "+" << std::endl; cout<<"ID "<<omp_get_thread_num()<<" "; } #pragma omp section //第一个section,由某个线程单独完成 for (int j = 0; j < 5; ++j) { std::cout<<"j " << j << "-" << std::endl; cout<<"ID "<<omp_get_thread_num()<<" "; } } }
原文:http://blog.csdn.net/sunboyiris/article/details/23611723