在编写多线程程序时,默认是所有线程之间的数据共享内存,并且需要指定私有内容。是否可以将所有数据声明为私有?
此致 -Mohd
答案 0 :(得分:0)
您可能希望查看thread local storage。
答案 1 :(得分:0)
你可以,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任务不会异常运行: