首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >openmp中的数据共享

openmp中的数据共享
EN

Stack Overflow用户
提问于 2013-09-10 00:51:06
回答 2查看 424关注 0票数 0

在编写多线程程序时,默认的做法是在所有线程之间共享内存,并且需要指定什么是私有的。是否可以将所有数据声明为私有数据?

致敬,-Mohd

EN

回答 2

Stack Overflow用户

发布于 2013-09-10 00:53:41

你可能想看看thread local storage

票数 0
EN

Stack Overflow用户

发布于 2013-09-10 01:17:59

你可以的,for example

代码语言:javascript
复制
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) 
{
int nthreads, tid;

/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
  {

  /* Obtain thread number */
  tid = omp_get_thread_num();
  printf("Hello World from thread = %d\n", tid);

  /* Only master thread does this */
  if (tid == 0) 
    {
    nthreads = omp_get_num_threads();
    printf("Number of threads = %d\n", nthreads);
    }

  }  /* All threads join master thread and disband */

}

但是,不能将共享数据替换为异步消息,因为不能保证openmp任务异步运行:

Use Threads Correctly = Isolation + Asynchronous Messages

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18703159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档