在编写多线程程序时,默认的做法是在所有线程之间共享内存,并且需要指定什么是私有的。是否可以将所有数据声明为私有数据?
致敬,-Mohd
发布于 2013-09-10 00:53:41
你可能想看看thread local storage。
发布于 2013-09-10 01:17:59
你可以的,for example
#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
https://stackoverflow.com/questions/18703159
复制相似问题