tbb并发hashmap实现字典ADT
tbb concurrent hashmap to implement dictionary ADT
我正在尝试使用TBB的并发哈希映射来实现字典ADT。我的顺序版本有问题。所以我想我使用map函数的方式有问题。gdb
表示代码挂在对erase(key)
的调用中,而CCD_2又调用lock
例程。闻起来像僵局。这是更正的代码:
#include<stdio.h>
#include "tbb/concurrent_hash_map.h"
using namespace tbb;
using namespace std;
typedef concurrent_hash_map<unsigned long,bool> tbbMap;
tbbMap map;
int findPercent;
int insertPercent;
int deletePercent;
unsigned long keyRange;
unsigned int lseed;
bool search(unsigned long key)
{
if(map.count(key))
{
return true;
}
else
{
return false;
}
}
bool insert(unsigned long key)
{
if(map.insert(std::make_pair(key,true)))
{
return true;
}
else
{
return(false);
}
}
bool remove(unsigned long key)
{
if(map.erase(key))
{
return true;
}
else
{
return(false);
}
}
void operateOnDictionary()
{
int chooseOperation;
unsigned long key;
int count=0;
while(count<10)
{
chooseOperation = rand_r(&lseed)%100;
key = rand_r(&lseed)%keyRange + 1;
if(chooseOperation < findPercent)
{
search(key);
}
else if (chooseOperation < insertPercent)
{
insert(key);
}
else
{
remove(key);
}
count++;
}
printf("donen");
return;
}
int main()
{
findPercent = 10;
insertPercent= findPercent + 45;
deletePercent = insertPercent + 45;
keyRange = 5;
lseed = 0;
operateOnDictionary();
}
当然,这是访问器的错误用法。它应该是作用域的RAII对象,而不是全局对象。
实际发生的情况是,find()
或insert()
获取访问器中的锁,但由于访问器未被破坏且未释放锁,因此不会释放该锁。然后,erase()
尝试获取相同的锁和hag,因为它已经被获取了。
顺便说一句,如果您只需要检查密钥是否存在,并且不需要从中读取任何内容,请考虑使用count()
。此外,如果您不打算在插入后访问元素,请不要使用insert(with_accessor,key)
,请使用不使用访问器的insert( std::make_pair(key, value) )
。
处理访问器意味着一定的运行时开销,因为它本质上是按元素锁定的。例如,比较没有接入者的concurrent_unordered_map
和有接入者的CC D_ 11是不公平的。
相关文章:
- C++中的MSD基排序(字典序)
- C++中是否有任何函数等同于python中字典的get函数?
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- C++中类似Python字典的结构?
- C++ 按数值对元组<字符串、浮点数>然后按字典顺序排序的向量
- 使用C++对一个巨大文件中的文本行进行字典式外部排序
- 与C++std::map::insert行为相比,C#排序字典的效率
- 为字典cpp创建[]=运算符
- C++ 随机生成的字典单词将仅显示以 B 开头的单词
- 如何将字典添加到向量?
- 蟒蛇字典到C++映射
- 使用给定的字典构建C++翻译器?
- 在使用 std::sort() 和 lambda 函数按属性对 ADT 的向量进行排序时遇到问题
- C++类:如何使用字典["hello"]=20;像蟒蛇
- 如何在 c++ 中展平字典
- 在 c++ 中访问嵌套字典unordered_map<字符串、void*>?
- 如何将返回的 Python 字典转换为 C++ std::map<string,string>
- 拉丁语的二进制字典 - 如何构建或在哪里找到
- STL 中字典的最佳容器
- tbb并发hashmap实现字典ADT