tbb::concurrent_hash_map 抛出 SIGSEGV
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码有两个潜在的问题。
首先,如果
find()
没有找到指定的键,它将无法取消引用 a->second。您应该使用insert()
重写它,这将确保元素的存在或添加条件检查,例如:其次,您在访问器的锁下调用 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 withinsert()
which will ensure existence of the element or add condition checking like: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.