tbb::concurrent_hash_map 抛出 SIGSEGV

发布于 2024-09-25 15:04:40 字数 1038 浏览 0 评论 0原文

我正在 Windows 上使用 mingw32 运行一个使用 TBB 构建的小程序。它执行parallel_for。在我的对象的parallel_for 内部对concurrent_hash_map 对象进行更改。它开始运行,但后来当我尝试使用访问器时抛出 SIGSEGV。我不知道问题出在哪里。

我的对象:

class Foobar
{
public:
    Foobar(FoobarParent* rw) : _rw(rw)
    {
        _fooMap = &_rw->randomWalkers();
    }

    void operator() (const tbb::blocked_range<size_t>&r ) const
    {
        for(size_t i = r.begin(); i != r.end(); ++i)
        {
            apply(i);
        }
    }

private:
    void apply(int i) const
    {
        pointMap_t::accessor a;
        _fooMap->find(a, i);
        Point3D current = a->second;
        Point3D next = _rw->getNext(current);

        if (!_rw->hasConstraint(next))
        {
            return;
        }

        a->second = next;
    }

    FoobarParent* _rw;
    pointMap_t* _fooMap;
};

pointMap_t 定义为:

typedef tbb::concurrent_hash_map<int, Point3D> pointMap_t;

有人可以阐明这个问题吗?我是 TBB 新手。当apply方法调用a->秒时抛出该信号。

I'm running a small program built using TBB on Windows with mingw32. It does a parallel_for. Inside the parallel_for my object makes changes to a concurrent_hash_map object. It starts running but later throws a SIGSEGV when I try to use an accessor. I don't know where the problem is.

My object:

class Foobar
{
public:
    Foobar(FoobarParent* rw) : _rw(rw)
    {
        _fooMap = &_rw->randomWalkers();
    }

    void operator() (const tbb::blocked_range<size_t>&r ) const
    {
        for(size_t i = r.begin(); i != r.end(); ++i)
        {
            apply(i);
        }
    }

private:
    void apply(int i) const
    {
        pointMap_t::accessor a;
        _fooMap->find(a, i);
        Point3D current = a->second;
        Point3D next = _rw->getNext(current);

        if (!_rw->hasConstraint(next))
        {
            return;
        }

        a->second = next;
    }

    FoobarParent* _rw;
    pointMap_t* _fooMap;
};

pointMap_t is defined as:

typedef tbb::concurrent_hash_map<int, Point3D> pointMap_t;

Can someone shed a light on this issue? I'm new to TBB. The signal is thrown when the apply method calls a->second.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄与心 2024-10-02 15:04:40

这段代码有两个潜在的问题。

首先,如果 find() 没有找到指定的键,它将无法取消引用 a->second。您应该使用 insert() 重写它,这将确保元素的存在或添加条件检查,例如:

if( a ) // process it

其次,您在访问器的锁下调用 getNext 和 hasConstraint。调用该锁下的任何内容都是危险的,因为它内部可能有另一个锁或对 TBB 的调用,从而可能导致死锁或其他问题。

There are two potential problems in this code.

First, if find() does not find the specified key, it will fail to dereference a->second. You should rewrite it either with insert() which will ensure existence of the element or add condition checking like:

if( a ) // process it

Second, you call getNext and hasConstraint under the lock of the accessor. It is dangerous to call anything under the lock since it can have another lock inside or a call to TBB and thus can lead to deadlock or other problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文