A thread-safe hash map implementation in C++ with support for concurrent read and write operations.
This is an attempt to create an equivalent of Java's ConcurrentHashMap in C++. It is not a direct equivalent because the underlying concurrency models in Java and C++ are different.
This C++ library provides a ConcurrentHashMap class that allows multiple threads to perform read and write operations on a hash map concurrently.
- Thread-safe read/write operations
- Fine-grained locking with
std::shared_mutex
- Modern C++17 API with
std::optional
returns - Efficient collision handling using chaining
- Comprehensive unit tests (Google Test)
- CMake build system support
- Include the Library:
#include "ConcurrentHashMap.h"
- Create an Instance:
ConcurrentHashMap<std::string, int> concurrentMap;
- Perform Operations:
// Insert a key-value pair
concurrentMap.insert("one", 1);
// Retrieve the value for a key
if(auto value = concurrentMap.get("one"))
std::cout << "Value for key 'one': " << value << std::endl;
} else {
std::cout << "Key 'one' not found." << std::endl;
}
// Remove a key-value pair
concurrentMap.remove("one");
Print entire map structure using print()
.
Custom hash function support:
struct CustomHash { /* ... */ };
ConcurrentHashMap<KeyType, ValueType, CustomHash> customMap;
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --parallel
cd build && ctest --verbose
./ConcurrentHashMapTest --gtest_color=yes
Eray Öztürk (@diffstorm)
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to contribute, report issues, or suggest improvements!
Contributions welcome! Please:
- Fork the repository
- Create your feature branch
- Submit a Pull Request
- Ensure all tests pass