首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在push_back并行区域中使用向量OpenMP安全吗?

在push_back并行区域中使用向量OpenMP安全吗?
EN

Stack Overflow用户
提问于 2015-11-06 04:20:19
回答 1查看 1K关注 0票数 2

我知道我不应该在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.

我的密码在这里安全吗?星型是包含浮点数的基本数据结构。

代码语言:javascript
复制
        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);
                }
            }
        }
EN

回答 1

Stack Overflow用户

发布于 2015-11-06 06:24:39

OpenMP标准说,接近第2.7.1节的末尾

在循环区域内执行的抛出必须导致在循环区域的相同迭代中恢复执行,抛出异常的同一个线程必须捕获它。

因此,在push_back()中加上一个try-catch结构,它本身在critical区域内应该可以使您的代码安全。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33559432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档