ThreadPool.h 5.9 KB

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