Qt编写OpenMP程序--循环测试

本程序是在Ubuntu Linux环境下编写成的。OpenMp提供并行描述,可以充分使用计算机系统的CPU资源。

Qt项目与C++语言源程序:

1.Qt项目文件

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
#CONFIG -= qt

QMAKE_CXXFLAGS += -fopenmp

LIBS += -fopenmp

SOURCES += 
    main.cpp

2.C++语言程序

#include <omp.h>
#include <stdio.h>

void test (int n) {
    for(int i = 0; i < 1000000; ++i) {
        //do nothing, just waste time
    }
    printf("%d ", n);
}

int main(void)
{

    #pragma omp parallel for
    for(int i = 0; i < 20; ++i)
        test(i);
    printf("
");

    return 0;
}

3.运行结果


可以看出,运行时输出顺序与一般输出顺序是不一样的,说明是并发运行的。

原文地址:https://www.cnblogs.com/tigerisland/p/7564278.html