1
我知道我不應該從openmp並行區域內拋出任何未捕獲的異常。因此,我不認爲下面的循環是安全的,因爲vector的push_back函數可能會導致可能會拋出異常的內存重新分配。在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.
所以是我的代碼在這裏安全嗎? Star是一個包含浮點數的基本數據結構。
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);
}
}
}
如果拋出未捕獲的異常是不好的,爲什麼不把'push_back'放在'try-catch'中呢? –