#include <stdio.h>
#include <omp.h>
#include <ctime>
#include <iostream>
using namespace std;
void Delay(int time)//time*1000为秒数
{
clock_t now = clock();
while (clock() - now < time);
}
int
main()
{
#pragma omp parallel sections num_threads(4)
{
#pragma omp section // independent thread
{
int thr = omp_get_thread_num();
printf("section 1, thread %d - sleeping 1 second\n", thr);
Delay(10 * 1000);
printf("section 1 done\n");
}
#pragma omp section // independent thread
{
int thr = omp_get_thread_num();
printf("section 2, thread %d - sleeping 2 second\n", thr);
Delay(10 * 1000);
printf("section 2 done\n");
}
#pragma omp section // independent thread
{
int thr = omp_get_thread_num();
printf("section 3, thread %d - sleeping 3 second\n", thr);
Delay(10 * 1000);
printf("section 3 done\n");
}
//printf("not in omp section"); // error - code must be in section
}
return 0;
}
// XXX: what happens when we change num_threads to 2
多线程 任务加速 openmp
最新推荐文章于 2024-03-16 14:50:20 发布