libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
pool_thread.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_PRIVATE_POOL_THREAD_HPP
2 #define LIBJMMCG_CORE_PRIVATE_POOL_THREAD_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2004 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "thread_base.hpp"
23 
24 #include "../../core/config.h"
25 
26 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace ppd { namespace pool { namespace private_ {
27 
28  /// This class of threads is used within the various thread_pool classes.
29  /**
30  Implementation details:
31  1. It uses a shared exit event (which is owned by the thread pool class). This optimisation allows parallel exiting of the threads within the pool. Thus the destructor of the pool is faster.
32  */
33  template<
34  typename OST,
35  class ExitReq ///< \todo is this really needed?
36  >
37  class pool_thread : public ppd::private_::thread_base<OST::thread_traits::api_params_type::api_type, typename OST::thread_traits::model_type> {
38  public:
39  using base_t=ppd::private_::thread_base<OST::thread_traits::api_params_type::api_type, typename OST::thread_traits::model_type>;
40  using os_traits=OST;
41  using model_type=typename OST::thread_traits::model_type;
42  using exit_requested_type=ExitReq; ///< The exit event type.
43 
44  static constexpr generic_traits::api_type api_type=OST::thread_traits::api_params_type::api_type;
45 
46  ~pool_thread() noexcept(false) FORCE_INLINE {}
47 
48  const tstring __fastcall to_string() const noexcept(false) override final {
49  tostringstream str;
50  str<<base_t::to_string()
51  <<_T("\nexit_requested signal ptr=")<<&exit_requested_;
52  return str.str();
53  }
54 
55  protected:
56  /// This event is used by the pool to request that all thread members of the pool should exit.
57  exit_requested_type &exit_requested_;
58 
59  explicit __stdcall pool_thread(exit_requested_type &exit_r, const typename os_traits::thread_traits::api_params_type::suspend_period_ms exit_wait_p=50) noexcept(false) FORCE_INLINE
60  : base_t(exit_wait_p), exit_requested_(exit_r) {
61  }
62 
63  private:
64  /**
65  Pool threads should only exit when the thread_pool exits.
66  */
67  void __fastcall request_exit() const override {}
68  };
69 
70  /// These are the various thread-types that are used within the various thread pools.
71  namespace thread_types {
72  /// For work-stealing type thread_pools.
73  template<generic_traits::return_data RD_, class OST, class PTT, class QM>
74  class steal;
75 
76  /// For master-slave type thread_pools.
77  template<generic_traits::return_data RD_, class OST, class PTT>
78  class slave;
79  }
80 
81 } } } } }
82 
83 #include "../../core/thread_pool_aspects.hpp"
84 
85 #ifdef WIN32
86 # include "../../experimental/NT-based/NTSpecific/pool_thread.hpp"
87 #else
88 # include "../../unix/pool_thread.hpp"
89 #endif
90 
91 #endif