Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Thread-Safe Map

Does anyone know where I can find an implimentation that wraps a std::map and makes it thread safe? When I say thread safe I mean that it offers only serial access to the map, one thread at a time. Optimally, this map should use only the standard-library and / or boost constructs.

like image 916
Chris Andrews Avatar asked May 04 '09 15:05

Chris Andrews


People also ask

What is a thread-safe map?

So what does the thread-safe Map means? If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally to avoid an inconsistent view of the contents.

Is map C++ thread-safe?

It isn't thread safe, insert from two threads and you can end up in an inconstant state.

Is map Get thread-safe?

Maps are naturally one of the most widely style of Java collection. And, importantly, HashMap is not a thread-safe implementation, while Hashtable does provide thread-safety by synchronizing operations. Even though Hashtable is thread safe, it is not very efficient. Another fully synchronized Map, Collections.

What is thread-safe C?

Thread safety A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface. In C language, local variables are dynamically allocated on the stack.


2 Answers

Does not meet the criteria that you have specified, but you could have a look at the TBB containers. There is so called concurrent_hash_map which allows multiple threads to access concurrently the data in the map. There are some details, but everything is nicely documented and can give you an idea of the "concurrent container". Depending on your needs this might be totally inappropriate...

like image 83
Anonymous Avatar answered Oct 08 '22 21:10

Anonymous


The boost shared_mutex would provide the best multiple reader/single writer approach to wrapping a standard map given your constraints. I don't know of any "pre-built" implementations that marry these two since the task is generally trivial.

like image 23
Joe Avatar answered Oct 08 '22 22:10

Joe