// OpenMP1.cpp : 定义控制台应用程序的入口点。
//sections和section指令
#include "stdafx.h"
#include "omp.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
#pragma omp parallel sections
{
#pragma omp section
printf("section 1 threadid=%d\n",omp_get_thread_num());
#pragma omp section
printf("section 2 threadid=%d\n",omp_get_thread_num());
#pragma omp section
printf("section 3 threadid=%d\n",omp_get_thread_num());
#pragma omp section
printf("section 4 threadid=%d\n",omp_get_thread_num());
}
system("pause");
return 0;
}
运行结果:
学习心得:
sections将一个任务分成独立的几个section,每个由不同的线程并行处理。
// OpenMP1.cpp : 定义控制台应用程序的入口点。
//多个sections指令
#include "stdafx.h"
#include "omp.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
#pragma omp parallel
{
#pragma omp sections
{
#pragma omp section
printf("section 1 threadid=%d\n",omp_get_thread_num());
#pragma omp section
printf("section 2 threadid=%d\n",omp_get_thread_num());
}
#pragma omp sections
{
#pragma omp section
printf("section 3 threadid=%d\n",omp_get_thread_num());
#pragma omp section
printf("section 4 threadid=%d\n",omp_get_thread_num());
}
}
system("pause");
return 0;
}
运行结果:
学习心得:
两个sections串行执行,第二个sections要等第一个sections执行完后才能执行。