What is the best way to implement stream ordered (note1) mapping / hash in C ++? Aka, a fast-growing data structure (aka, not a queue), with which different flows can pass, sometimes inserting or deleting elements without interfering with the activity of other flows?
std :: map is not thread safe, and its operations are non-atomic - although only erasing cancels iterators
Wrapping each function in the entire map class does not fix the problem - you may have free iterators pointing to a node that is being erased by another thread. It should either block or prevent deletion until the current thread is the only one referencing it, or use the "saggy but still valid delete link" file system UNIX style
- tbb :: concurrent_hash_map is designed for thread safety, but its iterators are still invalid when deleting their key. Even if the value is a smart pointer, the data will not be saved, since the link in the iterator will be lost.
- You can use tbb: concurrent_hash_map by iterating through the keys instead of iterators (you can search for the key as O (1) rather than O (log N) like std :: map), but since it is a hash, it lacks order-specific ones like upper_bound, lower_bound, etc. Critically, if the key is left dangling by erasing in another thread, there is no obvious way to say that the code will return to the closest element to this key in the hash.
- std :: unordered_map has the ability so that you can try to set the oath on the nearest element if your key is deleted through the bucket access functions. But std :: unordered_map is not considered thread safe (although it could potentially be hooked as thread safe). tbb :: concurrent_hash_map is thread safe (subject to the above restrictions), but for this, sufficient access to the buckets is not allowed.
- std:: map, , , . O (log N) O (1).
- , , std:: map . . , , , .
- - , , , std:: map ( ), , , /.
, ?
** 1: "", , " , ", " ". , , , ( ). , , , , . /...
** 2: . ... std:: map ? , (, std:: shared_ptr), , , //. , " : ". A) ( = 0), , (operator =, operator ++, operator-- ..) , ; B) ( = false), . , , , .
, , ( //), , , , , . - ?