1.点击项目->属性
在OpenMp支持那里,写上 是(/openmp)
注意:(/openmp)里面的o和m必须是小写的!
2.程序代码
注意:如果写#include "stdafx.h"时,报错:无法找到预编译文件stdafx.h那就直接将这句话去掉即可!
#include<Windows.h>
#include<tchar.h>
#include<stdio.h>
#include "omp.h"
int _tmain(int argc, _TCHAR *argv[]) {
printf("Hello from serial. Thread number = % d\n", omp_get_thread_num());
#pragma omp parallel num_threads(4)
{
printf("Hello from parallel. Thread number = %d\n", omp_get_thread_num());
}
printf("Hello from serial again\n");
system("pause");
return 0;
}
运行结果:
(这里如果能运行出四个不同的Thread number就说明有四个线程在并行工作,并且每次的编译结果都是不同的)