Skip to content

A thread-safe hash map implementation in C++ with support for concurrent read and write operations.

License

Notifications You must be signed in to change notification settings

diffstorm/ConcurrentHashMap

Repository files navigation

ConcurrentHashMap C++ Library Awesome

CI License: MIT C++ Standard Code Coverage GitHub Stars Platforms

A thread-safe hash map implementation in C++ with support for concurrent read and write operations.

Overview

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.

Features

  • 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

Usage

  1. Include the Library:
#include "ConcurrentHashMap.h"
  1. Create an Instance:
ConcurrentHashMap<std::string, int> concurrentMap;
  1. 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");

Advanced Features

Print entire map structure using print().

Custom hash function support:

struct CustomHash { /* ... */ };
ConcurrentHashMap<KeyType, ValueType, CustomHash> customMap;

Build

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --parallel

Running tests

After CMake build

cd build && ctest --verbose

Or directly

./ConcurrentHashMapTest --gtest_color=yes

⛄ Author

Eray Öztürk (@diffstorm)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Feel free to contribute, report issues, or suggest improvements!

Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create your feature branch
  3. Submit a Pull Request
  4. Ensure all tests pass

About

A thread-safe hash map implementation in C++ with support for concurrent read and write operations.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published