我知道我不应该在openmp并行区域内抛出任何未被察觉的异常。因此,我不认为下面的循环是安全的,因为向量的push_back函数可能导致内存的重新分配,这可能会引发异常。
我查了一下文档,上面写着If a reallocation happens, the strong guarantee is also given if the type of the elements is either copyable or no-throw moveable.
我的密码在这里安全吗?星型是包含浮点数的基本数据结构。
std::vector<Star> stars;
#pragma omp parallel for
for(size_t i = 1; i < (gimg.height() - 1) * 2; i++)
{
float i_ = i / 2.0f;
for(float j = 1; j < gimg.width() - 1; j += 0.5f)
{
//a b c
//d e f
//g h k
quantum_t e = gimg.bilinear(j, i_);
quantum_t a = gimg.bilinear(j - 1, i_ - 1);
if(a >= e) continue;
quantum_t b = gimg.bilinear(j, i_ - 1);
if(b >= e) continue;
quantum_t c = gimg.bilinear(j + 1, i_ + 1);
if(c >= e) continue;
quantum_t d = gimg.bilinear(j - 1, i_);
if(d >= e) continue;
quantum_t f = gimg.bilinear(j + 1, i_);
if(f >= e) continue;
quantum_t g = gimg.bilinear(j - 1, i_ + 1);
if(g >= e) continue;
quantum_t h = gimg.bilinear(j, i_ + 1);
if(h >= e) continue;
quantum_t k = gimg.bilinear(j + 1, i_ + 1);
if(k >= e) continue;
bool ismax = d >= a && d >= g && h >= g && h >= k && f >= k && f >= c && b >= c && b >= a;
if(ismax)
{
Star s;
s.brightness = e;
s.location = Point<float>(j, i_);
#pragma omp critical
stars.push_back(s);
}
}
}发布于 2015-11-06 06:24:39
OpenMP标准说,接近第2.7.1节的末尾
在循环区域内执行的抛出必须导致在循环区域的相同迭代中恢复执行,抛出异常的同一个线程必须捕获它。
因此,在push_back()中加上一个try-catch结构,它本身在critical区域内应该可以使您的代码安全。
https://stackoverflow.com/questions/33559432
复制相似问题