OpenDDS  Snapshot(2023/04/28-20:55)
ThreadPool.h
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #ifndef OPENDDS_DCPS_THREAD_POOL_H
9 #define OPENDDS_DCPS_THREAD_POOL_H
10 
11 #include "dcps_export.h"
12 
13 #include "ConditionVariable.h"
14 #include "PoolAllocator.h"
15 
16 #include <ace/Thread.h>
17 
18 #if defined ACE_HAS_PTHREADS && defined ACE_LACKS_PTHREAD_JOIN
19 #define OPENDDS_NO_THREAD_JOIN
20 #endif
21 
23 
24 namespace OpenDDS {
25 namespace DCPS {
26 
27 /**
28  * ThreadPool is a light-weight utility class for starting a group of threads
29  *
30  * ThreadPool creates several threads at construction and attempts to join them
31  * at destruction. Users of ThreadPool are responsible for making sure the
32  * running threads are in a joinable state before the destruction of ThreadPool
33  */
35 {
36 public:
37 
38  /// A typedef for the starting point of the ThreadPool
39  typedef ACE_THR_FUNC_RETURN (*FunPtr)(void*);
40 
41  /**
42  * Creates a ThreadPool with the specifed size, starting point, and argument
43  * @param count number of threads
44  * @param fun starting point for the threads of the ThreadPool
45  * @param arg an optional argument to pass to the starting function
46  */
47  ThreadPool(size_t count, FunPtr fun, void* arg = 0);
48  virtual ~ThreadPool();
49 
50  /// A static helper function used to redirect to requested thread start point
51  static ACE_THR_FUNC_RETURN run(void* arg);
52 
53  /**
54  * Check if a specific thread id belongs to this ThreadPool
55  * @param id thread id to check
56  * @returns true if the ThreadPool contains thread with specified id, false otherwise
57  */
58  bool contains(ACE_thread_t id) const;
59 
60 private:
61 
62  void join_all();
63 
64  FunPtr fun_;
65  void* arg_;
70 #if defined OPENDDS_NO_THREAD_JOIN
71  size_t finished_threads_;
72 #endif
74  OPENDDS_SET(ACE_thread_t) id_set_;
75 };
76 
77 } // DCPS
78 } // OpenDDS
79 
81 
82 #endif // OPENDDS_DCPS_THREAD_POOL_H
ConditionVariable< ACE_Thread_Mutex > cv_
Definition: ThreadPool.h:67
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
ThreadStatusManager tsm_
Definition: ThreadPool.h:68
HANDLE ACE_hthread_t
DWORD ACE_thread_t
ACE_Thread_Mutex mutex_
Definition: ThreadPool.h:66
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
typedef OPENDDS_VECTOR(ActionConnectionRecord) ConnectionRecords
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
typedef OPENDDS_SET(NetworkAddress) AddrSet