The problem is caused by where you have the counter code. I tried this myself and got a matching counts when I moved the counter to outside the reader thread's scope. Something like this :
int main ()
{
int N = 3174014;
std::nullptr_t NULLPOINTER = nullptr;
HashMap hashMap(NULLPOINTER);
test( N, hashMap );
std::cout << "------------finish write---------------" << std::endl;
size_t hashmap_size = hashMap.size();
std::cout << "\n------ hashMap size = " << hashmap_size << std::endl;
{
int thread_num = 32;
std::thread reader(
[&] () {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
auto readStartTime = std::chrono::high_resolution_clock::now();
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(0, N - 1);
for (int i = 0; i < 10; i++) {
#pragma omp parallel for num_threads(thread_num)
for (int i = 0; i < N; i++) {
A* value;
int randomKey = dis(gen);
if(hashMap.find(randomKey, value)){
}
}
}
auto readEndTime = std::chrono::high_resolution_clock::now();
double readTime = std::chrono::duration<double>(readEndTime
- readStartTime).count();
std::cout << "-----readTime=" << readTime << std::endl;
}
);
reader.join();
}
size_t cnt = 0;
for( auto it1 = hashMap.begin(); it1 != hashMap.end(); ++it1 )
{
cnt++;
}
std::cout << " cnt=" << cnt << std::endl;
if( cnt == hashmap_size )
std::cout << "counts match" << std::endl;
else
std::cout << "error" << std::endl;
hashMap.clear();
return 0;
}