1. demo代码
#include <iostream>
#include <thread>
#include <unordered_map>
#include "stdlib.h"
using namespace std;
unordered_map<int, char*> testMap;
void UpdateMap(int threadId)
{
while(1) {
testMap.clear();
this_thread::sleep_for(chrono::milliseconds(10));
int randomKey = rand() % 50;
cout<< "thread: "<< threadId << " "<< "key:" <<randomKey <<endl;
if (testMap.find(randomKey) == testMap.end()) {
testMap.insert(pair<int, char*>(randomKey, nullptr));
} else {
// testMap.erase(randomKey);
}
}
}
int main() {
constexpr int threadNum = 2;
std::cout << "Hello, World!" << std::endl;
thread t[threadNum];
for(int i =0; i<threadNum; i++)
{
t[i] = thread(UpdateMap,i);
}
for(auto&i:t) i.join();