简体   繁体   中英

C++ Multithreading Error : no matching function for call to 'std::thread::thread(<unresolved overloaded function type>

The console gives the below error:

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*********\n\t 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 << '\n\n';

/*******************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;
}

You may do it like this:

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

I assume the error is about the missing copy constructor. Thread's can only be moved.

I would use the standard function async: https://www.cplusplus.com/reference/future/async/ instead of spawning threads directly as it matches your use case more do just to do something async once and not repeatedly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM