C++ omp 代码仅在一个线程上运行
C++ omp code working on only one thread
我是OpenMP编程新手,我有一个问题。我找到了一个使用 OpenMP 的简单代码示例。代码生成成功,但代码仅在一个线程上运行。有一个代码:
#include <iostream>
#include <omp.h>
int main (int argc, const char * argv[])
{
int nProcessors=omp_get_max_threads();
std::cout<<nProcessors<<std::endl;
omp_set_num_threads(nProcessors);
std::cout<<omp_get_num_threads()<<std::endl;
#pragma omp parallel for
for(int i=0;i<5;i++){
int tid=omp_get_thread_num();
std::cout<<tid<<"t tid"<<std::endl;
int nThreads=omp_get_num_threads();
std::cout<<nThreads<<"t nThreads"<<std::endl;
}
exit(0);
}
它返回该结果:
4
1
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
Press any key to continue . . .
任何人都可以帮助我做错了什么?顺便说一下,我使用的是英特尔 i5 CPU。
谢谢亚历克斯,
我找到了一个解决方案,我需要做的一切都是在项目属性中启用 OMP:
项目属性>> C/C++>> 语言>>打开 MP 支持 = 是
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- 在另一个线程中调用luaL_error会引发qWarning
- C++一个线程如何正确通信其任务已完成?
- 最佳做法是从另一个线程访问 qml 中的Q_PROPERTY
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 如何制作一个只能在一个线程上同时执行的函数?
- 结束另一个线程中使用的对象的生存期
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 一个线程等待多个线程事件
- 从不同进程中的另一个线程挂起/恢复线程或进程
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 计时器是否从另一个线程启动?
- 互斥,Windows 10,c ++,在一个线程上获取,在另一个线程上发布
- Qt 在另一个线程中无限循环
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- 当只有一个线程主要使用该对象而其他线程很少使用它时,如何最小化该对象的互斥锁锁定?
- 从另一个线程发出信号是否安全?
- AMQP-CPP,libev >停止ev_loop来自另一个线程