thread_safe_map.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @brief
  3. 线程安全模板类
  4. * @version
  5. V 1.0.0
  6. * @author
  7. 王益俊
  8. * @date
  9. 创建时间: 2018-08-20\n
  10. * @note
  11. 2018-08-20 创建类。\n
  12. * @warning
  13. * @bug
  14. */
  15. #pragma once
  16. #include <map>
  17. #include <stdint.h>
  18. #include <boost/thread/mutex.hpp>
  19. template<class Key, class T>
  20. class thread_safe_map
  21. {
  22. private:
  23. std::map<Key, T> __map;
  24. mutable boost::mutex the_mutex;
  25. public:
  26. /**
  27. * @brief
  28. 重置函数。
  29. * @param [in] const Key &inputKey key\n
  30. * @param [in] const T &inputValue value\n
  31. * @return 无\n
  32. * @note
  33. * @warning
  34. * @bug
  35. */
  36. void insert( const Key &inputKey, const T &inputValue )
  37. {
  38. boost::mutex::scoped_lock lock( the_mutex );
  39. __map.insert( std::pair<Key, T>( inputKey, inputValue ) );
  40. lock.unlock();
  41. }
  42. /**
  43. * @brief
  44. 返回map是否为空函数。
  45. * @param 无\n
  46. * @return 是否为空\n
  47. * @retval true 为空\n
  48. * @retval false 不为空\n
  49. * @note
  50. * @warning
  51. * @bug
  52. */
  53. bool empty() const
  54. {
  55. boost::mutex::scoped_lock lock( the_mutex );
  56. return __map.empty();
  57. }
  58. /**
  59. * @brief
  60. 用某个key获取值的函数。
  61. * @param [in] const Key &inputKey 指定的key\n
  62. * @param [out] T &outputValue 获得的值\n
  63. * @return 是否获取成功\n
  64. * @retval true 成功\n
  65. * @retval false 失败\n
  66. * @note
  67. * @warning
  68. * @bug
  69. */
  70. bool get( const Key &inputKey, T &outputValue )
  71. {
  72. boost::mutex::scoped_lock lock( the_mutex );
  73. typename std::map<Key, T>::iterator it;
  74. it = __map.find( inputKey );
  75. if ( __map.end() == it )
  76. {
  77. return false;
  78. }
  79. outputValue = it->second;
  80. return true;
  81. }
  82. /**
  83. * @brief
  84. 用查找某个key是否存在的函数。
  85. * @param [in] const Key &inputKey 指定的key\n
  86. * @return 是否存在\n
  87. * @retval true 存在\n
  88. * @retval false 不存在\n
  89. * @note
  90. * @warning
  91. * @bug
  92. */
  93. bool seek( const Key &inputKey )
  94. {
  95. boost::mutex::scoped_lock lock( the_mutex );
  96. typename std::map<Key, T>::iterator it;
  97. it = __map.find( inputKey );
  98. if ( __map.end() == it )
  99. {
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * @brief
  106. 更新某个key对应值的函数。
  107. * @param [in] const Key &inputKey 指定的key\n
  108. * @param [out] T &outputValue 获得的值\n
  109. * @return 是否获取成功\n
  110. * @retval true 成功\n
  111. * @retval false 失败\n
  112. * @note
  113. * @warning
  114. * @bug
  115. */
  116. bool update( const Key &inputKey, const T &inputValue )
  117. {
  118. boost::mutex::scoped_lock lock( the_mutex );
  119. typename std::map<Key, T>::iterator it;
  120. it = __map.find( inputKey );
  121. if ( __map.end() == it )
  122. {
  123. return false;
  124. }
  125. it->second = inputValue;
  126. return true;
  127. }
  128. /**
  129. * @brief
  130. 根据某个key删除值的函数。
  131. * @param [in] const Key &inputKey 指定的key\n
  132. * @return 无\n
  133. * @note
  134. * @warning
  135. * @bug
  136. */
  137. void erase( const Key &inputKey )
  138. {
  139. boost::mutex::scoped_lock lock( the_mutex );
  140. __map.erase( inputKey );
  141. }
  142. /**
  143. * @brief
  144. 获得大小的函数。
  145. * @param 无\n
  146. * @return 无\n
  147. * @note
  148. * @warning
  149. * @bug
  150. */
  151. size_t size() const
  152. {
  153. boost::mutex::scoped_lock lock( the_mutex );
  154. return __map.size();
  155. }
  156. /**
  157. * @brief
  158. 清除map的函数。
  159. * @param 无\n
  160. * @return 无\n
  161. * @note
  162. * @warning
  163. * @bug
  164. */
  165. void clear()
  166. {
  167. boost::mutex::scoped_lock lock( the_mutex );
  168. __map.clear();
  169. }
  170. /**
  171. * @brief
  172. 拷贝map的函数。
  173. * @param 无\n
  174. * @return 无\n
  175. * @note
  176. * @warning
  177. * @bug
  178. */
  179. void copy( std::map<Key, T>& dest_map )
  180. {
  181. boost::mutex::scoped_lock lock( the_mutex );
  182. dest_map = __map;
  183. }
  184. };