For both windows and linux:
In multi-threaded application, in case I do not care the very exactly when getting the count of a hash_map, then can I safely call {hash_map}.size() while still allow other threads to add/delete items on that {hash_map}?
Thank you.
No
The STL containers (if that’s what you’re using) are not thread-safe.
size() is likely to walk the buckets and count the numbers. Modifying that data structure while another thread walks it is dangerous (that is, dangerous like a drunk elephant in a minefield).
I’d recommend wrapping your hash_map with some functions that update the count separately, as an atomic integer. That value will not be exact, but probably will be close enough, and it will reduce thread contention between size() and insert/erase operations.