ThreadPool.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // ThreadPool.h
  3. //
  4. // Library: Foundation
  5. // Package: Threading
  6. // Module: ThreadPool
  7. //
  8. // Definition of the ThreadPool class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_ThreadPool_INCLUDED
  16. #define Foundation_ThreadPool_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Thread.h"
  19. #include "Poco/Mutex.h"
  20. #include <vector>
  21. namespace Poco {
  22. class Runnable;
  23. class PooledThread;
  24. class Foundation_API ThreadPool
  25. /// A thread pool always keeps a number of threads running, ready
  26. /// to accept work.
  27. /// Creating and starting a threads can impose a significant runtime
  28. /// overhead to an application. A thread pool helps to improve
  29. /// the performance of an application by reducing the number
  30. /// of threads that have to be created (and destroyed again).
  31. /// Threads in a thread pool are re-used once they become
  32. /// available again.
  33. /// The thread pool always keeps a minimum number of threads
  34. /// running. If the demans for threads increases, additional
  35. /// threads are created. Once the demand for threads sinks
  36. /// again, no-longer used threads are stopped and removed
  37. /// from the pool.
  38. {
  39. public:
  40. ThreadPool(int minCapacity = 2,
  41. int maxCapacity = 16,
  42. int idleTime = 60,
  43. int stackSize = POCO_THREAD_STACK_SIZE);
  44. /// Creates a thread pool with minCapacity threads.
  45. /// If required, up to maxCapacity threads are created
  46. /// a NoThreadAvailableException exception is thrown.
  47. /// If a thread is running idle for more than idleTime seconds,
  48. /// and more than minCapacity threads are running, the thread
  49. /// is killed. Threads are created with given stack size.
  50. ThreadPool(const std::string& name,
  51. int minCapacity = 2,
  52. int maxCapacity = 16,
  53. int idleTime = 60,
  54. int stackSize = POCO_THREAD_STACK_SIZE);
  55. /// Creates a thread pool with the given name and minCapacity threads.
  56. /// If required, up to maxCapacity threads are created
  57. /// a NoThreadAvailableException exception is thrown.
  58. /// If a thread is running idle for more than idleTime seconds,
  59. /// and more than minCapacity threads are running, the thread
  60. /// is killed. Threads are created with given stack size.
  61. ~ThreadPool();
  62. /// Currently running threads will remain active
  63. /// until they complete.
  64. void addCapacity(int n);
  65. /// Increases (or decreases, if n is negative)
  66. /// the maximum number of threads.
  67. int capacity() const;
  68. /// Returns the maximum capacity of threads.
  69. void setStackSize(int stackSize);
  70. /// Sets the stack size for threads.
  71. /// New stack size applies only for newly created threads.
  72. int getStackSize() const;
  73. /// Returns the stack size used to create new threads.
  74. int used() const;
  75. /// Returns the number of currently used threads.
  76. int allocated() const;
  77. /// Returns the number of currently allocated threads.
  78. int available() const;
  79. /// Returns the number available threads.
  80. void start(Runnable& target);
  81. /// Obtains a thread and starts the target.
  82. /// Throws a NoThreadAvailableException if no more
  83. /// threads are available.
  84. void start(Runnable& target, const std::string& name);
  85. /// Obtains a thread and starts the target.
  86. /// Assigns the given name to the thread.
  87. /// Throws a NoThreadAvailableException if no more
  88. /// threads are available.
  89. void startWithPriority(Thread::Priority priority, Runnable& target);
  90. /// Obtains a thread, adjusts the thread's priority, and starts the target.
  91. /// Throws a NoThreadAvailableException if no more
  92. /// threads are available.
  93. void startWithPriority(Thread::Priority priority, Runnable& target, const std::string& name);
  94. /// Obtains a thread, adjusts the thread's priority, and starts the target.
  95. /// Assigns the given name to the thread.
  96. /// Throws a NoThreadAvailableException if no more
  97. /// threads are available.
  98. void stopAll();
  99. /// Stops all running threads and waits for their completion.
  100. ///
  101. /// Will also delete all thread objects.
  102. /// If used, this method should be the last action before
  103. /// the thread pool is deleted.
  104. ///
  105. /// Note: If a thread fails to stop within 10 seconds
  106. /// (due to a programming error, for example), the
  107. /// underlying thread object will not be deleted and
  108. /// this method will return anyway. This allows for a
  109. /// more or less graceful shutdown in case of a misbehaving
  110. /// thread.
  111. void joinAll();
  112. /// Waits for all threads to complete.
  113. ///
  114. /// Note that this will not actually join() the underlying
  115. /// thread, but rather wait for the thread's runnables
  116. /// to finish.
  117. void collect();
  118. /// Stops and removes no longer used threads from the
  119. /// thread pool. Can be called at various times in an
  120. /// application's life time to help the thread pool
  121. /// manage its threads. Calling this method is optional,
  122. /// as the thread pool is also implicitly managed in
  123. /// calls to start(), addCapacity() and joinAll().
  124. const std::string& name() const;
  125. /// Returns the name of the thread pool,
  126. /// or an empty string if no name has been
  127. /// specified in the constructor.
  128. static ThreadPool& defaultPool();
  129. /// Returns a reference to the default
  130. /// thread pool.
  131. protected:
  132. PooledThread* getThread();
  133. PooledThread* createThread();
  134. void housekeep();
  135. private:
  136. ThreadPool(const ThreadPool& pool);
  137. ThreadPool& operator = (const ThreadPool& pool);
  138. typedef std::vector<PooledThread*> ThreadVec;
  139. std::string _name;
  140. int _minCapacity;
  141. int _maxCapacity;
  142. int _idleTime;
  143. int _serial;
  144. int _age;
  145. int _stackSize;
  146. ThreadVec _threads;
  147. mutable FastMutex _mutex;
  148. };
  149. //
  150. // inlines
  151. //
  152. inline void ThreadPool::setStackSize(int stackSize)
  153. {
  154. _stackSize = stackSize;
  155. }
  156. inline int ThreadPool::getStackSize() const
  157. {
  158. return _stackSize;
  159. }
  160. inline const std::string& ThreadPool::name() const
  161. {
  162. return _name;
  163. }
  164. } // namespace Poco
  165. #endif // Foundation_ThreadPool_INCLUDED