task制导
显式地将代码块作为任务创建,并将这些任务分配给线程池中的线程执行,多个任务可以并行执行,每个任务都可能在不同的线程上执行。通过#pragma omp task
指令来创建任务,以及通过#pragma omp taskwait
指令来等待任务的完成。
#include <iostream>
#include <omp.h>
using namespace std;
void func(int task){
cout << "thread " << omp_get_thread_num() << " is excuting task " << task << endl;
}
int main(int argc, char* argv[]){
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task
{
func(1);
}
#pragma omp task
{
func(2);
}
#pragma omp taskwait
cout << "taskwait..." << endl;
#pragma omp task
{
func(3);
}
}
}
return 0;
}
task
thread 5 is excuting task 2
thread 4 is excuting task 1
taskwait...
thread 0 is excuting task 3
no taskwait
taskwait...
thread 3 is excuting task 3
thread 0 is excuting task 1
thread 4 is excuting task 2
。。。
标签:task,thread,taskwait,任务,线程,team,OpenMP,excuting From: https://www.cnblogs.com/tao-gak567/p/18077051