// // Created by zx on 2020/7/3. // #ifndef NNXX_TESTS_THREAD_SAFE_MAP_H #define NNXX_TESTS_THREAD_SAFE_MAP_H #include #include template class thread_safe_map { public: typedef typename std::map::iterator this_iterator; typedef typename std::map::const_iterator this_const_iterator; Val& operator [](const Key& key) { std::lock_guard lk(mtx_); return dataMap_[key]; } int erase(const Key& key ) { std::lock_guard lk(mtx_); return dataMap_.erase(key); } this_iterator find( const Key& key ) { std::lock_guard lk(mtx_); return dataMap_.find(key); } this_const_iterator find( const Key& key ) const { std::lock_guard lk(mtx_); return dataMap_.find(key); } this_iterator end() { return dataMap_.end(); } this_const_iterator end() const { return dataMap_.end(); } private: std::map dataMap_; std::mutex mtx_; }; #endif //NNXX_TESTS_THREAD_SAFE_MAP_H