TBB task_group使用concurrent_hash_map时挂起
TBB task_group hang when using concurrent_hash_map
我试图在正在运行的任务中使用 tbb::concurrent_hash_map,但我遇到了调用地图的 erase() 会导致任务无限锁定的问题。任何想法下面的代码片段可能有什么问题?
#include <iostream>
#include <boost/date_time.hpp>
#include <tbb/concurrent_hash_map.h>
#include <tbb/task_group.h>
#include <tbb/task_scheduler_init.h>
class BusyTask
{
public:
void operator()() {
typedef tbb::concurrent_hash_map<unsigned int, int> MyMap;
MyMap m;
MyMap::accessor a;
m.insert(a, 1);
m.erase(1); // The task will lock up at this point
}
};
int main(int argc, char* argv[])
{
std::cout << "Started" << std::endl;
BusyTask busyTask1;
tbb::task_group taskGroup;
taskGroup.run(busyTask1);
taskGroup.wait();
std::cout << "Finished" << std::endl;
return 0;
}
我正在使用TBB v4.0.5和GCC 4.7进行测试
正如评论中正确指出的那样,这是锁定范围的问题。 erase(1)
需要获取已经通过 insert() 操作获取的相同锁(并且锁不是递归的)。
还请注意哈希映射的 erase(by_accessor) 方法,该方法保证受访问器保护的元素将被擦除,而不是具有相同键的另一个元素。如果并发线程擦除了它(提供相同的密钥)并添加了具有相同密钥的新元素,则可能会发生后者。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 什么是"#include <boost/functional/hash.hpp> "?
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 使用字符数组作为 Map 中的键
- C++如何创建 std::map
- C++ equivalent to Java Map getOrDefault?
- 从其他容器中移动构造"std::map"
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?