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.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
You might look at Thread Safe Template Library
This is up to the application to implement. A "thread-safe" map would make individual calls into the map thread-safe, but many operations need to be made thread-safe across calls. The application that uses the map should associate a mutex with the map, and use that mutex to coordinate accesses to it.
Trying to make thread-safe containers was a mistake in Java, and it would be a mistake in C++.
There is a proposition here (by me - shameless plug) that wraps objects (including
STL
containers) for efficient (zero-cost) thread safe access:https://github.com/isocpp/CppCoreGuidelines/issues/924
Implementation code can be found here:
https://github.com/galik/GSL/blob/lockable-objects/include/gsl/gsl_lockable
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...Try this library
http://www.codeproject.com/KB/threads/lwsync.aspx
It is implemented in a modern c++ policy based approach.
Here is some cut from the link to show the idea with the 'vector' case
I came up with this (which I'm sure can be improved to take more than two arguments):
This allows you to do:
If you wish to use an std::recursive_mutex and an std::set, that would also work.