// Thread 1:
void awesome_function() {
if (my_map.empty()) { //! call size() internally;
}
}
//! Thread 2:
void another_awesome_function() {
//! Clear internally lock size_ with a mutex data race occur in the thread 1 because size_ is not locked
my_map.clear(); //!
}
Potential solution (size() function can lock size_):
https://github.com/facebook/folly/blob/f6ac9ac15bce487b8a87b2a9e06949eb55074e99/folly/concurrency/detail/ConcurrentHashMap-detail.h#L247
Hello the following use case will data-race:
Potential solution (size() function can lock size_):
Where size_ is locked ?
Another solution can be to have an std::atomic and returning size_.load(); when necessary.