lundi 26 novembre 2018

c++: Error: No instance of overloaded function using std::map

#include <iostream>
#include <mutex>
#include <map>
#include <thread>

using namespace std;

//Global variable
std::mutex mu; //declare a mutex
std::map<std::string, int> threadIDs;

void run(int id) {
    std::unique_lock<std::mutex> map_locker(mu); 
    threadIDs.insert(std::make_pair(std::this_thread::get_id(), id));
    map_locker.unlock();
}

int main()
{
    std::thread t[5];
    for (int i = 0; i < 5; i++) {
        t[i] = std::thread(run, i);
    }
    for (int i = 0; i < 5; i++) {
        t[i].join();
    }
    return 0;    
}//end of the code

Hello, I am trying to execute 5 thread running the void run() function and save thread id and int value using std::map. However I get red underline underneath the '.' in threadIDs.insert(std::make_pair(std::this_thread::get_id(), id)); line saying no instance of overloaded function... I guess the error occurs because std::map wants a string and an int inside but I am trying to put std::this_thread::get_id() in the string place. How can I put the thread identity inside the std::map?

Aucun commentaire:

Enregistrer un commentaire