1.CmakeLists.txt文件
# Enable OpenMP
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(YourTargetName PUBLIC OpenMP::OpenMP_CXX)
endif()
2.cpp文件
#include <iostream>
#include <omp.h>
void test()
{
#if _OPENMP
std::cout << " support openmp " << std::endl;
#else
std::cout << " not support openmp" << std::endl;
#endif
int sum = 0;
#pragma omp parallel for num_threads(4) reduction(+:sum)
for(int i=0; i<100; i++)
{
sum += i;
}
std::cout << " sum = " << sum << std::endl;
sum = 0;
#pragma omp parallel num_threads(4)
{
#pragma omp atomic
sum += 10;
}
std::cout << " sum2 = " << sum << std::endl;
}