螺纹将其插入BIMAP
Threading to insert into a bimap
我想使用多个线程插入bimap
。我尝试了以下代码:
parallel_index.cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <thread>
#include <mutex>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/unordered_multiset_of.hpp>
#include "parallel_index.h"
namespace bimaps = boost::bimaps;
typedef boost::bimap<bimaps::unordered_set_of<uint64_t>,
bimaps::unordered_multiset_of<std::string> > bimap_reference;
typedef bimap_reference::value_type position;
bimap_reference reference_index_vector;
size_t total_threads = std::thread::hardware_concurrency();
std::string sequence_content = "ABCDDBACDDDCBBAAACBDAADCBDAAADCBDADADACBDDCBBBCDCBCDAADCBBCDAAADCBDA";
uint64_t sequence_length = sequence_content.length();
int split = 5;
uint64_t erase_length = 0;
unsigned int seq_itr = 0;
std::mutex mtx; // to protect against concurent access
int main(){
thread_test::create_index index;
std::thread threads[total_threads-1];
for(unsigned int i = 0; i < total_threads; i++){
threads[i] = std::thread(&thread_test::create_index::reference_index_hash, std::ref(index), sequence_length, split, sequence_content, erase_length);
}
for(unsigned int i = 0; i < total_threads; i++){
threads[i].join();
}
}
/*
* Creating index
*/
void thread_test::create_index::reference_index_hash(uint64_t &sequence_length, int &split,
std::string &sequence_content, uint64_t &erase_length ){
for (; seq_itr < sequence_length; ++seq_itr ){
std::string splitstr = sequence_content.substr(erase_length, split);
std::lock_guard<std::mutex> lck(mtx);
reference_index_vector.insert(position(seq_itr, splitstr));
seq_itr += split-1;
erase_length += split;
// the length of test is very large so erase some text
if(erase_length > 10000){
sequence_content.erase(0,erase_length);
erase_length = 0;
}
}
// for( bimap_reference::const_iterator iter = reference_index_vector.begin(), iend = reference_index_vector.end();
// iter != iend; ++iter ) {
// std::cout << iter->left << " <--> "<< iter->right <<std::endl;
// }
}
parallel_index.h
#ifndef PARALLEL_INDEX_H_
#define PARALLEL_INDEX_H_
#include<iostream>
#include <algorithm>
#include <utility>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/unordered_multiset_of.hpp>
//typedef boost::unordered_map<int, std::pair<int, unsigned long int>& > reference_map;
namespace bimaps = boost::bimaps;
typedef boost::bimap<bimaps::unordered_set_of<uint64_t>,
bimaps::unordered_multiset_of<std::string > > bimap_reference;
typedef bimap_reference::value_type position;
extern bimap_reference reference_index_vector;
namespace thread_test{
class create_index{
public:
void reference_index_hash(uint64_t &sequence_length, int &split,
std::string &sequence_content, uint64_t &erase_length);
};
}
#endif /* PARALLEL_INDEX_H_ */
我使用Eclipse IDE编译代码,但是我会得到错误
g++ -std=c++17 -I/home/ahussain/boost_1_65_1 -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"parallel_index.d" -MT"parallel_index.o" -o "parallel_index.o" "../parallel_index.cpp"
In file included from /usr/include/c++/5/thread:39:0,
from ../parallel_index.cpp:13:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (thread_test::create_index::*)(long unsigned int&, int&, std::__cxx11::basic_string<char>&, long unsigned int&)>(std::reference_wrapper<thread_test::create_index>, long unsigned int, int, std::__cxx11::basic_string<char>, long unsigned int)>’:
/usr/include/c++/5/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (thread_test::create_index::*)(long unsigned int&, int&, std::__cxx11::basic_string<char>&, long unsigned int&); _Args = {std::reference_wrapper<thread_test::create_index>, long unsigned int&, int&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, long unsigned int&}]’
../parallel_index.cpp:42:149: required from here
/usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (thread_test::create_index::*)(long unsigned int&, int&, std::__cxx11::basic_string<char>&, long unsigned int&)>(std::reference_wrapper<thread_test::create_index>, long unsigned int, int, std::__cxx11::basic_string<char>, long unsigned int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (thread_test::create_index::*)(long unsigned int&, int&, std::__cxx11::basic_string<char>&, long unsigned int&)>(std::reference_wrapper<thread_test::create_index>, long unsigned int, int, std::__cxx11::basic_string<char>, long unsigned int)>’
_M_invoke(_Index_tuple<_Indices...>)
^
subdir.mk:18: recipe for target 'parallel_index.o' failed
make: *** [parallel_index.o] Error 1
我在这里看了看,但无法摆脱错误。
index
可以作为简单的指针而不是包裹的参考传递,但是剩余的变量预计为参考,因此它们需要std::ref
处理。
换句话说
threads[i] = std::thread(&thread_test::create_index::reference_index_hash,
&index,
std::ref(sequence_length),
std::ref(split),
std::ref(sequence_content),
std::ref(erase_length));
将编译。
但是...
不幸的是,这意味着您将拥有多个线程,所有线程都在相同的变量中工作。这通常是一个非启动器,因为这些线程将对变量存储的值同时访问和造成严重破坏。您将不得不重新考虑如何使用这些变量,并通过价值通过它们或找到更好的方法来隔离或保护它们。
和...
std::thread threads[total_threads-1];
与
结合for(unsigned int i = 0; i < total_threads; i++)
将导致一个太多的thread
S进入数组。0至total_threads
是total_threads
thread
S的计数,而不是total_threads-1
。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 插入或删除时获取usb的dos_name
- 叮叮当当在修复时插入多个"覆盖"说明符
- 链表c++插入,所有情况都已检查,但没有任何工作
- 将重物插入std::map
- 螺纹将其插入BIMAP
- 将 boost::d ynamic_bitset<> 插入 boost::bimap
- 使用boost::关联属性映射插入boost::BIMAP..失败
- 将std::map复制/插入到boost::bimap中