简体   繁体   English

如何在OpenMP C ++中分配动态内存

[英]How allocate dynamic memory in OpenMP C++

I tried to parallel function, that allocates memory, but I had an exception of bad heap. 我尝试并行化功能,该功能分配内存,但是我有一个坏堆的例外。 Memory must have used some threads in one time. 内存必须一次使用一些线程。

void GetDoubleParameters( CInd *ci )
{
    for(int i=0;i<ci->size();i++)
    {
        void *tmp;
        #pragma omp parallel private (tmp)
        {
            for(int j=0;j<ci[i].getValues().size();j++)
            {
                tmp = (void*)new double(ci[i].getValues()[j]);
                ci->getParameters().push_back(tmp);
            }
        }

    }
}

The problem is the line: 问题是这一行:

ci->getParameters().push_back(tmp);

ci is accessed by all parallel threads at once, and its parameters element with the push_back routine (probably a std::vector) is probably not thread-safe. ci被所有并行线程一次访问,并且带有push_back例程(可能是std :: vector)的parameter元素可能不是线程安全的。 You will have to organize some guards around this code. 您将必须围绕此代码组织一些防护措施。 Something like: 就像是:

omp_lock_t my_lock;
...
// initialize lock
omp_init_lock (&my_lock);
...

// do something sensible in parallel
  ...
  {
      omp_guard my_guard (my_lock);
      // protected region starts here
      // only one thread at a time works here
  }
  // more parallel work
  ...
}
omp_destroy_lock (&my_lock);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM