ThreadPool.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // ThreadPool.h
  3. //
  4. // $Id: //poco/svn/Foundation/include/Poco/ThreadPool.h#2 $
  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. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_ThreadPool_INCLUDED
  38. #define Foundation_ThreadPool_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/Thread.h"
  41. #include "Poco/Mutex.h"
  42. #include <vector>
  43. namespace Poco {
  44. class Runnable;
  45. class PooledThread;
  46. class Foundation_API ThreadPool
  47. /// A thread pool always keeps a number of threads running, ready
  48. /// to accept work.
  49. /// Creating and starting a threads can impose a significant runtime
  50. /// overhead to an application. A thread pool helps to improve
  51. /// the performance of an application by reducing the number
  52. /// of threads that have to be created (and destroyed again).
  53. /// Threads in a thread pool are re-used once they become
  54. /// available again.
  55. /// The thread pool always keeps a minimum number of threads
  56. /// running. If the demans for threads increases, additional
  57. /// threads are created. Once the demand for threads sinks
  58. /// again, no-longer used threads are stopped and removed
  59. /// from the pool.
  60. {
  61. public:
  62. ThreadPool(int minCapacity = 2,
  63. int maxCapacity = 16,
  64. int idleTime = 60,
  65. int stackSize = POCO_THREAD_STACK_SIZE);
  66. ThreadPool(const std::string& name,
  67. int minCapacity = 2,
  68. int maxCapacity = 16,
  69. int idleTime = 60,
  70. int stackSize = POCO_THREAD_STACK_SIZE);
  71. /// Creates a thread pool with minCapacity threads.
  72. /// If required, up to maxCapacity threads are created
  73. /// a NoThreadAvailableException exception is thrown.
  74. /// If a thread is running idle for more than idleTime seconds,
  75. /// and more than minCapacity threads are running, the thread
  76. /// is killed. Threads are created with given stack size.
  77. ~ThreadPool();
  78. /// Currently running threads will remain active
  79. /// until they complete.
  80. void addCapacity(int n);
  81. /// Increases (or decreases, if n is negative)
  82. /// the maximum number of threads.
  83. int capacity() const;
  84. /// Returns the maximum capacity of threads.
  85. void setStackSize(int stackSize);
  86. /// Sets the stack size for threads.
  87. /// New stack size applies only for newly created threads.
  88. int getStackSize() const;
  89. /// Returns the stack size used to create new threads.
  90. int used() const;
  91. /// Returns the number of currently used threads.
  92. int allocated() const;
  93. /// Returns the number of currently allocated threads.
  94. int available() const;
  95. /// Returns the number available threads.
  96. void start(Runnable& target);
  97. /// Obtains a thread and starts the target.
  98. /// Throws a NoThreadAvailableException if no more
  99. /// threads are available.
  100. void start(Runnable& target, const std::string& name);
  101. /// Obtains a thread and starts the target.
  102. /// Assigns the given name to the thread.
  103. /// Throws a NoThreadAvailableException if no more
  104. /// threads are available.
  105. void startWithPriority(Thread::Priority priority, Runnable& target);
  106. /// Obtains a thread, adjusts the thread's priority, and starts the target.
  107. /// Throws a NoThreadAvailableException if no more
  108. /// threads are available.
  109. void startWithPriority(Thread::Priority priority, Runnable& target, const std::string& name);
  110. /// Obtains a thread, adjusts the thread's priority, and starts the target.
  111. /// Assigns the given name to the thread.
  112. /// Throws a NoThreadAvailableException if no more
  113. /// threads are available.
  114. void stopAll();
  115. /// Stops all running threads.
  116. /// Will also delete all thread objects.
  117. /// If used, this method should be the last action before
  118. /// the thread pool is deleted.
  119. void joinAll();
  120. /// Waits for all threads to complete.
  121. void collect();
  122. /// Stops and removes no longer used threads from the
  123. /// thread pool. Can be called at various times in an
  124. /// application's life time to help the thread pool
  125. /// manage its threads. Calling this method is optional,
  126. /// as the thread pool is also implicitly managed in
  127. /// calls to start(), addCapacity() and joinAll().
  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. // inlines
  150. inline void ThreadPool::setStackSize(int stackSize)
  151. {
  152. _stackSize = stackSize;
  153. }
  154. inline int ThreadPool::getStackSize() const
  155. {
  156. return _stackSize;
  157. }
  158. } // namespace Poco
  159. #endif // Foundation_ThreadPool_INCLUDED