一个线程安全的std::map封装
来源:互联网 发布:christopher bu知乎 编辑:程序博客网 时间:2024/05/17 23:55
#pragma once#include <map>#include <stdint.h>#include <opencv2/opencv.hpp>#include <boost/thread/mutex.hpp>#include <boost/thread/condition_variable.hpp>template<class Key, class T>class concurrent_map{private: std::map<Key, T> the_map; mutable boost::mutex the_mutex; boost::condition_variable the_condition_variable;public: void insert(const Key &inputKey, const T &inputValue) { boost::mutex::scoped_lock lock(the_mutex); the_map.insert(std::pair<Key, T>(inputKey, inputValue)); lock.unlock(); the_condition_variable.notify_all(); } bool empty() const { boost::mutex::scoped_lock lock(the_mutex); return the_map.empty(); } bool try_get(const Key &inputKey, T &outputValue) { boost::mutex::scoped_lock lock(the_mutex); typename std::map<Key, T>::iterator it; it = the_map.find(inputKey); if(the_map.end() == it) { return false; } outputValue = it->second; return true; } void wait_and_get(const Key &inputKey, T &outputValue) { boost::mutex::scoped_lock lock(the_mutex); typename std::map<Key, T>::iterator it; while(the_map.end() == (it = the_map.find(inputKey))) { the_condition_variable.wait(lock); } outputValue = it->second; } void wait_next_insert() { boost::mutex::scoped_lock lock(the_mutex); the_condition_variable.wait(lock); } void erase(const Key &inputKey) { boost::mutex::scoped_lock lock(the_mutex); the_map.erase(inputKey); } size_t size() const { boost::mutex::scoped_lock lock(the_mutex); return the_map.size(); }};
阅读全文
0 0
- 一个线程安全的std::map封装
- 如何实现一个线程安全的map?
- C++11:基于std::queue和std::mutex构建一个线程安全的队列
- C++11:基于std::unordered_map和共享锁构建线程安全的map
- c++ 中 设计一个线程安全的map
- 实现一个简单的线程安全c++ map
- std::string 线程安全
- Map的线程安全实现
- 一个仿Java类封装的std::list封装类
- 线程安全的的map-CSuperMap
- Std::map的排序
- std::map的使用
- std::map的用法
- std::map的使用
- std::map的机制
- std::map的使用方法
- std::map的使用
- Map线程安全
- 编译错误 无法打开包括文件:“SDKDDKVer.h”: No such file or directory
- 【09】一个程序员的简洁职业生涯规划
- C4.5算法的理解
- 20170908_TCP的短连接和长连接
- Qt 正则匹配
- 一个线程安全的std::map封装
- for表达式的语句执行顺序?
- 基于Android Studio的Android内存泄漏检测方法
- JavaScript窗口
- C++中堆和栈的介绍
- python爬虫
- Unable to load native-hadoop library spark-cluster模式相关错误
- 算法导论之动态规划之最大子数组
- nginx搭建图片服务器