C++ 多线程错误:没有匹配的函数来调用'std::thread::thread(<未解析的重载函数类型>



控制台给出以下错误:

brute.cpp: In function 'void createThreads(int, int, MAP&, std::string)':
../brute.cpp:125:67: error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, std::reference_wrapper<std::unordered_map<std::basic_string<char>, std::basic_string<char> > >, std::string&)'
125 |     threadVec.push_back(std::thread(remove, std::ref(bufObj), hash));
/***************** Method to check if the current buffer has a match of the given hash ***********/
void remove(MAP &buffer, std::string hash) {
while (buffer.size() > 0)
{
const auto itrHash = buffer.find(hash);
if (itrHash == buffer.end()) {
buffer.erase(itrHash, buffer.end());
}
else {
std::cout.flush();
std::cout << "t*********nt SUCCESS :: The Password of the Hash to Check is " << itrHash->first << " is " << itrHash->second;
cracked = true;
}
}
return;
}
/***** Method to accumulate a hash and the respective string value to a unordered set of <string, string>
***********************/
void accumulate_hash(MAP &buffer, int beginIndex, int endIndex, int wordLen){
while (beginIndex < endIndex) {
std::cout << charList[beginIndex] <<'n';
std::string prefix(1,charList[beginIndex]);
std::cout << prefix << 'n';
printAllKLengthRec(charList, prefix, sizeofArr, wordLen - 1, buffer);
beginIndex++;
}
}
/*@brief The method that equally divides the work between threads
* @params wordLen The length of the word
* @params threadCount Based on number of Cores
* @buffObj That stores all the hashs and the respective values
* @&threads Reference to list of Threads
**/
void createThreads(int wordLen, int threadCount, MAP &bufObj, std::string hash) {
int  index = 0;
int charListSize = std::strlen(charList);
int charSetSize = charListSize / threadCount;
int difference = charListSize % threadCount;
int equallydividedWork = charSetSize;
std::vector<std::thread> threadVec;
std::cout<< hash << 'nn';
/*******************Below Line is where the error is generated ********************/
threadVec.push_back(std::thread(remove, std::ref(bufObj), hash));
/*********************** ---------------------------------- **********************/

// The character list is divided into equal set of characters
// Each character Set is then used to generate strings that creates hashes
int i = 0;
while (index < charListSize) {
if (charSetSize == 0)
charSetSize = 1;
if(i + 1 == threadCount)
charSetSize += difference;
/***************Compiler Does not have issue with the below line though *********************/
// Threads are created
threadVec.push_back(std::thread(accumulate_hash, std::ref(bufObj), index, charSetSize, wordLen));
/********************* ---------------------------------------------------**********/

index = charSetSize;
charSetSize = charSetSize + equallydividedWork;
if (charSetSize >= charListSize) {
charSetSize = charListSize;
}
i++;
}
return;
}

您可以这样做:

std::thread tmpThread{ remove, std::ref(bufObj), hash };
threadVec.push_back(std::move(tmpThread));

我认为错误是由于缺少复制构造函数。线程只能移动。

我会使用标准函数async:https://www.cplusplus.com/reference/future/async/与其在匹配用例时直接生成线程,不如只做一次异步操作,而不是重复执行。

相关内容

  • 没有找到相关文章

最新更新