并行计算—OpenMP—临界区critical

来源:互联网 发布:linux反编译class文件 编辑:程序博客网 时间:2024/05/19 20:38
// OpenMP1.cpp : 定义控制台应用程序的入口点。//在临界区寻找正整数数组的最大元素#include "stdafx.h"#include "omp.h"#include <iostream>#include <stdlib.h>int _tmain(int argc, _TCHAR* argv[]){int max=0; int a[10]={11,2,33,49,113,20,321,250,689,16};    #pragma omp parallel forfor(int j=0;j<10;j++){int temp=a[j];        #pragma omp critical{if(temp>max)max=temp;}}    std::cout<<"max:"<<max<<std::endl;system("pause");return 0;}

运行结果:

max:689

学习心得:

    critical指定某一区域的代码,每次只能同时被一个线程执行。



0 0
原创粉丝点击