【发布时间】:2018-09-02 17:49:26
【问题描述】:
我正在尝试使用 tbb 的 concurrent_hash_map 来提高我的应用程序的并发性能。阅读它并根据我的应用程序实现它,但我看到崩溃..
所以,我的应用程序是一个多线程应用程序,我在其中存储对,键是 char*,值是整数。伪代码如下:
在.h文件中,
typedef tbb::concurrent_hash_map<const void*, unsigned, Hasher> tbb_concurrent_hash;
tbb_concurrent_hash concurrent_hash_table;
tbb_concurrent_hash::accessor write_lock;
tbb_concurrent_hash::const_accessor read_lock;
在.c文件中,
void storeName(const char * name) {
int id=0;
// This creates a pair object of name and index
std::pair object(name, 0);
// read_lock is a const accessor for reading. This find function searches for char* in the table and if not found, create a write_lock.
bool found = concurrent_hash_table.find(read_lock, name);
if (found == FALSE) {
concurrent_hash_table.insert(write_lock, name);
// Get integer id somehow.
id = somefunction();
write_lock->second = id;
write_lock.release();
} else {
// if the name is found in the table then get the value and release it later
id = read_lock->second;
read_lock.release();
}
}
根据我的理解,我对实现很满意,但正如我所说,当 find 返回 FALSE 时会发生多次崩溃。崩溃也有互斥的痕迹。
【问题讨论】:
-
为什么会有
read_lock和write_lock?你能说明它们是如何声明的吗? -
@orhtej2,编辑了问题。抱歉没有早点写。干杯!
-
另请参阅此文档 - 我认为您可以简化您的解决方案:threadingbuildingblocks.org/docs/help/index.htm#reference/…
-
@WillBickford,你建议什么样的简化?超载?我以为我有相当简单的实现。
-
文档指出,如果您的密钥已经存在,
insert将返回 false,因此您应该能够完全消除查找,因为如果它不存在,您总是插入。
标签: c++ multithreading concurrency hashmap tbb