tbb::concurrent_hash_map-需要帮助才能开始
tbb::concurrent_hash_map - need help getting started
我正在尝试开始使用TBB。
我想实现一个并发散列映射(concurrent_hash_map)
它应该用一个长int键控,并返回一个char*。。。
这是我手头的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <gmp.h>
#include <sys/time.h>
#include <omp.h>
#include <iostream>
#include <string.h>
#include "tbb/concurrent_hash_map.h"
using namespace tbb;
using namespace std;
typedef concurrent_hash_map<long int,char*> table;
int main(int argc,char* argv[]){
/*what to do here?*/
/*How do I check if key present/add/remove entries to the hash, table?*/
return 0;
}
请记住,我对C++还很陌生。。。我已经为此奋斗了一个多小时。我已经阅读了以下链接:
http://www.devx.com/cplus/Article/33334/1763/page/2
https://stackoverflow.com/questions/7656329/how-to-lock-the-whole-concurrent-hash-map-not-warping-a-portion-of-code-with-mut
有人能告诉我正确的方向吗?
我从文档中得到了这个例子:
#include "tbb/concurrent_hash_map.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include <string>
using namespace tbb;
using namespace std;
// Structure that defines hashing and comparison operations for user's type.
struct MyHashCompare {
static size_t hash( const string& x ) {
size_t h = 0;
for( const char* s = x.c_str(); *s; ++s )
h = (h*17)^*s;
return h;
}
//! True if strings are equal
static bool equal( const string& x, const string& y ) {
return x==y;
}
};
// A concurrent hash table that maps strings to ints.
typedef concurrent_hash_map<string,int,MyHashCompare> StringTable;
// Function object for counting occurrences of strings.
struct Tally {
StringTable& table;
Tally( StringTable& table_ ) : table(table_) {}
void operator()( const blocked_range<string*> range ) const {
for( string* p=range.begin(); p!=range.end(); ++p ) {
StringTable::accessor a;
table.insert( a, *p );
a->second += 1;
}
}
};
const size_t N = 1000000;
string Data[N];
void CountOccurrences() {
// Construct empty table.
StringTable table;
// Put occurrences into the table
parallel_for( blocked_range<string*>( Data, Data+N, 1000 ),
Tally(table) );
// Display the occurrences
for( StringTable::iterator i=table.begin(); i!=table.end(); ++i )
printf("%s %dn",i->first.c_str(),i->second);
}
[编辑:作者的解决方案作为社区wiki答案移出]
现在可以工作了:
...
#include "tbb/concurrent_hash_map.h"
using namespace tbb;
using namespace std;
typedef concurrent_hash_map<long int,char*> data_hash;
int main(){
data_hash dh;
data_hash::accessor a;
long int k=10;
dh.insert(a,k);
a->second="hello";
for(data_hash::iterator j=dh.begin();j!=dh.end(); ++j){
printf("%lu %sn",j->first,j->second);
}
if(dh.find(a,9)){
printf("truen");
}else{
printf("falsen");
}
a.release();
return 0;
}
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 如何从头开始创建 avi 文件?
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 自一周开始以来的秒数?
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- GStreamer在开始任何播放之前进行搜索
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 嗨,我需要帮助构建具有特定要求的程序.我已经开始,但只需要帮助完成即可
- 需要帮助开始构建我自己的哈希表
- tbb::concurrent_hash_map-需要帮助才能开始
- 新手chrome插件开发人员需要帮助开始使用NPAPI
- 需要帮助开始c++程序
- 需要帮助开始使用Boost.Python