stack_cnc.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //----------------------------------------------------------------------------
  2. /// @file stack_cnc.hpp
  3. /// @brief This file contains the implementation concurrent stack
  4. ///
  5. /// @author Copyright (c) 2010 2015 Francisco José Tapia (fjtapia@gmail.com )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanyingfile LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. /// @version 0.1
  10. ///
  11. /// @remarks
  12. //-----------------------------------------------------------------------------
  13. #ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_STACK_CNC_HPP
  14. #define __BOOST_SORT_PARALLEL_DETAIL_UTIL_STACK_CNC_HPP
  15. #include <boost/sort/common/spinlock.hpp>
  16. #include <vector>
  17. namespace boost
  18. {
  19. namespace sort
  20. {
  21. namespace common
  22. {
  23. //
  24. //###########################################################################
  25. // ##
  26. // ################################################################ ##
  27. // # # ##
  28. // # C L A S S # ##
  29. // # S T A C K _ C N C # ##
  30. // # # ##
  31. // ################################################################ ##
  32. // ##
  33. //###########################################################################
  34. //
  35. //---------------------------------------------------------------------------
  36. /// @class stack_cnc
  37. /// @brief This class is a concurrent stack controled by a spin_lock
  38. /// @remarks
  39. //---------------------------------------------------------------------------
  40. template<typename T, typename Allocator = std::allocator<T> >
  41. class stack_cnc
  42. {
  43. public:
  44. //------------------------------------------------------------------------
  45. // D E F I N I T I O N S
  46. //------------------------------------------------------------------------
  47. typedef std::vector<T, Allocator> vector_t;
  48. typedef typename vector_t::size_type size_type;
  49. typedef typename vector_t::difference_type difference_type;
  50. typedef typename vector_t::value_type value_type;
  51. typedef typename vector_t::pointer pointer;
  52. typedef typename vector_t::const_pointer const_pointer;
  53. typedef typename vector_t::reference reference;
  54. typedef typename vector_t::const_reference const_reference;
  55. typedef typename vector_t::allocator_type allocator_type;
  56. typedef Allocator alloc_t;
  57. protected:
  58. //-------------------------------------------------------------------------
  59. // INTERNAL VARIABLES
  60. //-------------------------------------------------------------------------
  61. vector_t v_t;
  62. mutable spinlock_t spl;
  63. public:
  64. //
  65. //-------------------------------------------------------------------------
  66. // function : stack_cnc
  67. /// @brief constructor
  68. //-------------------------------------------------------------------------
  69. explicit stack_cnc(void): v_t() { };
  70. //
  71. //-------------------------------------------------------------------------
  72. // function : stack_cnc
  73. /// @brief Move constructor
  74. //-------------------------------------------------------------------------
  75. stack_cnc(stack_cnc &&) = delete;
  76. //
  77. //-------------------------------------------------------------------------
  78. // function : ~stack_cnc
  79. /// @brief Destructor
  80. //-------------------------------------------------------------------------
  81. virtual ~stack_cnc(void) { v_t.clear(); };
  82. //-------------------------------------------------------------------------
  83. // function : emplace_back
  84. /// @brief Insert one element in the back of the container
  85. /// @param args : group of arguments for to build the object to insert. Can
  86. /// be values, references or rvalues
  87. //-------------------------------------------------------------------------
  88. template<class ... Args>
  89. void emplace_back(Args &&... args)
  90. {
  91. std::lock_guard < spinlock_t > guard(spl);
  92. v_t.emplace_back(std::forward< Args > (args)...);
  93. };
  94. //
  95. //-------------------------------------------------------------------------
  96. // function :pop_move_back
  97. /// @brief if exist, move the last element to P, and delete it
  98. /// @param P : reference to a variable where move the element
  99. /// @return true - Element moved and deleted
  100. /// false - Empty stack_cnc
  101. //-------------------------------------------------------------------------
  102. bool pop_move_back(value_type &P)
  103. {
  104. std::lock_guard < spinlock_t > S(spl);
  105. if (v_t.size() == 0) return false;
  106. P = std::move(v_t.back());
  107. v_t.pop_back();
  108. return true;
  109. };
  110. //-------------------------------------------------------------------------
  111. // function : push_back
  112. /// @brief Insert one vector at the end of the container
  113. /// @param v_other : vector to insert
  114. /// @return reference to the stack_cnc after the insertion
  115. //-------------------------------------------------------------------------
  116. template<class Allocator2>
  117. stack_cnc &push_back(const std::vector<value_type, Allocator2> &v_other)
  118. {
  119. std::lock_guard < spinlock_t > guard(spl);
  120. for (size_type i = 0; i < v_other.size(); ++i)
  121. {
  122. v_t.push_back(v_other[i]);
  123. }
  124. return *this;
  125. };
  126. };
  127. // end class stack_cnc
  128. //***************************************************************************
  129. };// end namespace common
  130. };// end namespace sort
  131. };// end namespace boost
  132. //***************************************************************************
  133. #endif