一个线程安全的std::map封装
2017-09-08 15:42
239 查看
#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(); } };
相关文章推荐
- 如何创建一个线程安全的Map?
- 写了一个SpringMVC 怎么封装到Map里!以后要多多利用这种方法,自己脑子笨就记下来了。
- 用C#创建一个封装Google Map API的GoogleMap Web服务器控件(二)
- 一个仿Java类封装的std::list封装类
- BeanListHandler:(常用)将多条记录封装到一个装有JavaBean的List集合中;MapHandler: 将一条记录封装到一个Map集合中,Map的key是列名,Map的value就是
- C++11:基于std::unordered_map和共享锁构建线程安全的map
- C++标准库:std_map作为一个关联数组
- MapListHandler:将多条记录封装到一个Map集合中;ColumnListHandler;将某列的值封装到List集合中,例如一列name属性等
- c++ 中 设计一个线程安全的map
- ScalarHandler: 将单个值封装、 例如select count(*),求内容的条数;KeyedHandler:将多条记录封装到一个Map集合的Map集合中。并且外面的Map集合是可以指定的
- JDBC将结果封装成一个List或Map
- 模拟java 中map的功能封装一个有序的map
- 用C#创建一个封装Google Map API的GoogleMap Web服务器控件(一)
- 如何实现一个线程安全的map?
- 仿照std::map定制一个Dictionary
- 用C#创建一个封装Google Map API的GoogleMap Web服务器控件(一) (转)
- C++11:基于std::queue和std::mutex构建一个线程安全的队列
- 用C#创建一个封装Google Map API的GoogleMap Web服务器控件(二) (转)
- C++标准库:std_map作为一个…
- 自己项目用的一个控件,自己封装的