c - 在后台运行线程并使用 OpenMP 继续 main

标签 c multithreading openmp

这是我想要的示例。

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

void increment1(){
  int x;
  x=0;
  for(int i = 0; i<30000000000000;i++){
    ++x;
  }
  printf("%d\n",x );
}

int main(){
  #pragma omp parallel  
  {
    #pragma omp sections  
    {
      #pragma omp section
      { increment1(); }
    }
  }
  printf("Continue\n" );

  return 0;
}

如您所见,increment1() 需要太多时间才能完成。我的问题是如何在后台设置此任务并先打印 printf("Continue\n");message 然后 printf("%d\n",x ); 一旦 increment1() 完成。 也试过OpenMP run threads but continue main但它不起作用

最佳答案

OpenMP 不是这样工作的。通常,您使用 OpenMP 使 increment1 中的循环运行得更快,例如通过应用 #pragma omp parallel for。当然,具体的工作共享方法取决于您的实际功能。

如果您确实需要使用函数式并行,即同时运行不同的函数,所有 应该同时运行的函数(因此包括执行 printf< 的代码) 需要在 section(或 task)中。

关于c - 在后台运行线程并使用 OpenMP 继续 main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52851985/

相关文章:

c - Linux 中的每线程语言环境

c - 在头部被删除并重新创建后创建列表的第二个元素时出错

c++ - C linux 代理服务器

java - 寻找带有仿函数的并发映射

openmp - 当线程数量大于迭代次数时,如何调度?

c - 错误 : unknown type name '#include' #include <stdio. h>

c# - Thread.CurrentThread.CurrentCulture 在线程池内的线程中不工作

Python 多处理 : knowing the thread/CPU number

openmp - OpenMP 中的原子性和关键性有什么区别?

matlab - MATLAB 中的矢量化是如何工作的?