thread_safe_map.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * @param [out] T &outputValue 获得的值\n
  87. * @return 是否获取成功\n
  88. * @retval true 成功\n
  89. * @retval false 失败\n
  90. * @note
  91. * @warning
  92. * @bug
  93. */
  94. bool update( const Key &inputKey, const T &inputValue )
  95. {
  96. boost::mutex::scoped_lock lock( the_mutex );
  97. typename std::map<Key, T>::iterator it;
  98. it = __map.find( inputKey );
  99. if ( __map.end() == it )
  100. {
  101. return false;
  102. }
  103. it->second = inputValue;
  104. return true;
  105. }
  106. /**
  107. * @brief
  108. 根据某个key删除值的函数。
  109. * @param [in] const Key &inputKey 指定的key\n
  110. * @return 无\n
  111. * @note
  112. * @warning
  113. * @bug
  114. */
  115. void erase( const Key &inputKey )
  116. {
  117. boost::mutex::scoped_lock lock( the_mutex );
  118. __map.erase( inputKey );
  119. }
  120. /**
  121. * @brief
  122. 获得大小的函数。
  123. * @param 无\n
  124. * @return 无\n
  125. * @note
  126. * @warning
  127. * @bug
  128. */
  129. size_t size() const
  130. {
  131. boost::mutex::scoped_lock lock( the_mutex );
  132. return __map.size();
  133. }
  134. /**
  135. * @brief
  136. 清除map的函数。
  137. * @param 无\n
  138. * @return 无\n
  139. * @note
  140. * @warning
  141. * @bug
  142. */
  143. void clear()
  144. {
  145. boost::mutex::scoped_lock lock( the_mutex );
  146. __map.clear();
  147. }
  148. /**
  149. * @brief
  150. 拷贝map的函数。
  151. * @param 无\n
  152. * @return 无\n
  153. * @note
  154. * @warning
  155. * @bug
  156. */
  157. void copy( std::map<Key, T>& dest_map )
  158. {
  159. boost::mutex::scoped_lock lock( the_mutex );
  160. dest_map = __map;
  161. }
  162. };