【发布时间】:2015-02-19 18:37:11
【问题描述】:
我正在尝试使用 TBB 的并发哈希映射来实现字典 ADT。我在使用顺序版本时遇到问题。所以我想我使用地图功能的方式有问题。
gdb 表示代码在调用erase(key) 时挂起,而erase(key) 又调用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("done\n");
return;
}
int main()
{
findPercent = 10;
insertPercent= findPercent + 45;
deletePercent = insertPercent + 45;
keyRange = 5;
lseed = 0;
operateOnDictionary();
}
【问题讨论】:
标签: c++ concurrency hashmap tbb