NCBI C++ ToolKit
thread_pool.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef UTIL__THREAD_POOL__HPP
2 #define UTIL__THREAD_POOL__HPP
3 
4 /* $Id: thread_pool.hpp 99785 2023-05-10 17:50:50Z vasilche $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Denis Vakatov, Pavel Ivanov
30  *
31  */
32 
33 
34 /// @file thread_pool.hpp
35 /// Pool of generic task-executing threads.
36 ///
37 /// CThreadPool -- base implementation of pool of threads
38 /// CThreadPool_Thread -- base implementation of thread in pool of threads
39 /// CThreadPool_Task -- abstract task for executing in thread pool
40 /// CThreadPool_Controller
41 /// -- abstract class to control the number of threads
42 /// in pool of threads
43 
44 
45 // Old interfaces and classes
46 #include <util/thread_pool_old.hpp>
47 
48 
49 
50 /** @addtogroup ThreadedPools
51  *
52  * @{
53  */
54 
56 
57 
58 class CThreadPool;
59 class CThreadPool_Impl;
60 class CThreadPool_Task;
62 class CThreadPool_Thread;
65 
66 
67 
68 /// Abstract class for representing single task executing in pool of threads
69 /// To use this class in application you should inherit your own class from it
70 /// and define method Execute() - the main method where all task logic
71 /// executes.
72 /// Every single task can be executed (or canceled before execution)
73 /// only once and only in one pool.
74 
76 {
77 public:
78  /// Status of the task
79  enum EStatus {
80  eIdle, ///< has not been placed in queue yet
81  eQueued, ///< in the queue, awaiting execution
82  eExecuting, ///< being executed
83  eCompleted, ///< executed successfully
84  eFailed, ///< failure during execution
85  eCanceled ///< canceled - possible only if canceled before
86  ///< processing was started or if method Execute()
87  ///< returns result eCanceled
88  };
89 
90  /// Constructor
91  /// @param priority
92  /// Priority of the task - the smaller the priority,
93  /// the sooner the task will execute
94  CThreadPool_Task(unsigned int priority = 0);
95 
96  /// Do the actual job. Called by whichever thread handles this task.
97  /// @return
98  /// Result of task execution (the status will be set accordingly)
99  /// @note
100  /// Only 3 values are allowed: eCompleted, eFailed, eCanceled.
101  virtual EStatus Execute(void) = 0;
102 
103 
104  /// Cancel the task.
105  /// Equivalent to calling CThreadPool::CancelTask(task).
106  /// @note
107  /// If the task is executing it may not be canceled right away. It is
108  /// responsibility of method Execute() implementation to check
109  /// value of IsCancelRequested() periodically and finish its execution
110  /// when this value is TRUE.
111  /// @note
112  /// If the task has already finished its execution then do nothing.
113  void RequestToCancel(void);
114 
115  /// Check if cancellation of the task was requested
116  bool IsCancelRequested(void) const;
117 
118  /// Get status of the task
119  EStatus GetStatus(void) const;
120 
121  /// Check if task execution has been already finished
122  /// (successfully or not)
123  bool IsFinished(void) const;
124 
125  /// Get priority of the task
126  unsigned int GetPriority(void) const;
127 
128 protected:
129  /// Callback to notify on changes in the task status
130  /// @param old
131  /// Task status before the change. Current value can be obtained from
132  /// GetStatus().
133  ///
134  /// @note
135  /// Status eQueued is set before task is actually pushed to the queue.
136  /// After eQueued status eIdle can appear if
137  /// insertion into the queue failed because of timeout.
138  /// Status eCanceled will be set only in 2 cases:
139  /// - if task is not executing yet and RequestToCancel() called, or
140  /// - if method Execute() returned eCanceled.
141  /// To check if task cancellation is requested during its execution
142  /// use methods OnCancelRequested() or IsCancelRequested().
143  /// @sa OnCancelRequested(), IsCancelRequested(), GetStatus()
144  virtual void OnStatusChange(EStatus old);
145 
146  /// Callback to notify when cancellation of the task is requested
147  /// @sa OnStatusChange()
148  virtual void OnCancelRequested(void);
149 
150  /// Copy ctor
151  CThreadPool_Task(const CThreadPool_Task& other);
152 
153  /// Assignment
154  /// @note
155  /// There is a possible race condition if request is assigned
156  /// and added to the pool at the same time by different threads.
157  /// It is a responsibility of the derived class to avoid this race.
159 
160  /// The thread pool which accepted this task for execution
161  /// @sa CThreadPool::AddTask()
162  CThreadPool* GetPool(void) const;
163 
164  /// Destructor. Will be called from CRef.
165  virtual ~CThreadPool_Task(void);
166 
167 private:
168  friend class CThreadPool_Impl;
169 
170  /// Init all members in constructor
171  /// @param priority
172  /// Priority of the task
173  void x_Init(unsigned int priority);
174 
175  /// Set pool as owner of this task.
176  void x_SetOwner(CThreadPool_Impl* pool);
177 
178  /// Detach task from the pool (if insertion into the pool has failed).
179  void x_ResetOwner(void);
180 
181  /// Set task status
182  void x_SetStatus(EStatus new_status);
183 
184  /// Internal canceling of the task
185  void x_RequestToCancel(void);
186 
187  /// Flag indicating that the task is already added to some pool
189  /// Pool owning this task
190  atomic<CThreadPool_Impl*> m_Pool;
191  /// Priority of the task
192  unsigned int m_Priority;
193  /// Status of the task
194  atomic<EStatus> m_Status;
195  /// Flag indicating if cancellation of the task was already requested
196  atomic<bool> m_CancelRequested;
197 };
198 
199 
200 
201 /// Main class implementing functionality of pool of threads.
202 ///
203 /// @note This class can be safely used as a member of some other class or as
204 /// a scoped variable; for more details see the destructor comments.
205 /// @sa ~CThreadPool()
206 
208 {
209 public:
210  /// Constructor
211  /// @param queue_size
212  /// Maximum number of tasks waiting in the queue. If 0 then tasks
213  /// cannot be queued and are added only when there are threads
214  /// to process them. If greater than 0 and there will be attempt to add
215  /// new task over this maximum then method AddTask() will wait for the
216  /// given timeout for some empty space in the queue.
217  /// @param max_threads
218  /// Maximum number of threads allowed to be launched in the pool.
219  /// Value cannot be less than min_threads or equal to 0.
220  /// @param min_threads
221  /// Minimum number of threads that have to be launched even
222  /// if there are no tasks added. Value cannot be greater
223  /// than max_threads.
224  /// @param threads_mode
225  /// Running mode of all threads in thread pool. Values fRunDetached and
226  /// fRunAllowST are ignored.
227  ///
228  /// @sa AddTask()
229  CThreadPool(unsigned int queue_size,
230  unsigned int max_threads,
231  unsigned int min_threads = 2,
232  CThread::TRunMode threads_mode = CThread::fRunDefault);
233 
234  /// Add task to the pool for execution.
235  /// @note
236  /// The pool will acquire a CRef ownership to the task which it will
237  /// hold until the task goes out of the pool (when finished)
238  /// @param task
239  /// Task to add
240  /// @param timeout
241  /// Time to wait if the tasks queue has reached its maximum length.
242  /// If NULL, then wait indefinitely.
243  void AddTask(CThreadPool_Task* task, const CTimeSpan* timeout = NULL);
244 
245  /// Request to cancel the task and remove it from queue if it is there.
246  ///
247  /// @sa CThreadPool_Task::RequestToCancel()
248  void CancelTask(CThreadPool_Task* task);
249 
250  /// Destructor -- will wait for all its threads to finish with the timeout
251  /// set by CThreadPool::SetDestroyTimeout(); current default is 10 seconds.
252  /// If this timeout is not enough for threads to terminate CThreadPool
253  /// will be destroyed but all threads will finish later without any
254  /// "segmentation fault" errors because CThreadPool_Impl object will remain
255  /// in memory until all its threads finish.
256  /// @note
257  /// If this CThreadPool object is destroyed at the end of the
258  /// application and it will fail to finish all threads in destructor,
259  /// then all memory allocated by CThreadPool_Impl can be shown as leakage
260  /// in the memory checking tools (like Valgrind). To avoid these
261  /// leakages or for some other reasons to make sure that ThreadPool
262  /// finishes all its operations before the destructor you can call
263  /// method Abort() at any place in your application.
264  /// @sa Abort(), SetDestroyTimeout()
265  virtual ~CThreadPool(void);
266 
267 
268  //
269  // Extended functionality:
270  //
271 
272 
273  /// Abort all functions of the pool -- cancel all queued tasks, send
274  /// cancellation notifications to all currently executing tasks.
275  /// @note
276  /// The already executing tasks may take some time to actually finish
277  /// execution -- see CancelTasks() and
278  /// CThreadPool_Task::RequestToCancel()for details
279  /// @attention
280  /// This call renders the pool unusable in the sense that you must not
281  /// call any of its methods after that!
282  /// @param timeout
283  /// Maximum time to wait for the termination of the pooled threads.
284  /// If this time is not enough for all threads to terminate, the Abort()
285  /// method returns, and all threads are terminated in the background.
286  /// If NULL, then wait for as long as it takes for all threads to finish.
287  void Abort(const CTimeSpan* timeout = NULL);
288 
289 
290  /// Constructor with custom controller
291  /// @param queue_size
292  /// Maximum number of tasks waiting in the queue. If 0 then tasks
293  /// cannot be queued and are added only when there are threads
294  /// to process them. If greater than 0 and there will be attempt to add
295  /// new task over this maximum then method AddTask() will wait for the
296  /// given timeout for some empty space in the queue.
297  /// @param controller
298  /// Custom controller object that will be responsible for number
299  /// of threads in the pool, when new threads have to be launched and
300  /// old and unused threads have to be finished. Default controller
301  /// implementation (set for the pool in case of using other
302  /// constructor) is CThreadPool_Controller_PID class.
303  /// @param threads_mode
304  /// Running mode of all threads in thread pool. Values fRunDetached and
305  /// fRunAllowST are ignored.
306  CThreadPool(unsigned int queue_size,
307  CThreadPool_Controller* controller,
308  CThread::TRunMode threads_mode = CThread::fRunDefault);
309 
310  /// Set timeout to wait for all threads to finish before the pool
311  /// should be able to destroy.
312  /// Default value is 10 seconds
313  /// @note
314  /// This method is meant to be called very rarely. Because of that it is
315  /// implemented in non-threadsafe manner. While this method is working
316  /// it is not allowed to call itself or GetDestroyTimeout() in other
317  /// threads.
318  void SetDestroyTimeout(const CTimeSpan& timeout);
319 
320  /// Get timeout to wait for all threads to finish before the pool
321  /// will be able to destroy.
322  /// @note
323  /// This method is meant to be called very rarely. Because of that it is
324  /// implemented in non-threadsafe manner. While this method is working
325  /// (and after that if timeout is stored in some variable as reference)
326  /// it is not allowed to call SetDestroyTimeout() in other threads.
327  const CTimeSpan& GetDestroyTimeout(void) const;
328 
329  /// Binary flags indicating different possible options in what environment
330  /// the pool will execute exclusive task
331  ///
332  /// @sa TExclusiveFlags, RequestExclusiveExecution()
334  /// Do not allow to add new tasks to the pool during
335  /// exclusive task execution
336  fDoNotAllowNewTasks = (1 << 0),
337  /// Finish all threads currently running in the pool
338  fFlushThreads = (1 << 1),
339  /// Cancel all currently executing tasks
340  fCancelExecutingTasks = (1 << 2),
341  /// Cancel all tasks waiting in the queue and not yet executing
342  fCancelQueuedTasks = (1 << 3),
343  /// Execute all tasks waiting in the queue before execution
344  /// of exclusive task
345  fExecuteQueuedTasks = (1 << 4)
346  };
347  /// Type of bit-masked combination of several values from EExclusiveFlags
348  ///
349  /// @sa EExclusiveFlags, RequestExclusiveExecution()
350  typedef unsigned int TExclusiveFlags;
351 
352  /// Add the task for exclusive execution in the pool
353  /// By default the pool suspends all new and queued tasks processing,
354  /// finishes execution of all currently executing tasks and then executes
355  /// exclusive task in special thread devoted to this work. The environment
356  /// in which exclusive task executes can be modified by flags parameter.
357  /// This method does not wait for exclusive execution, it is just adds
358  /// the task to exclusive queue and starts the process of exclusive
359  /// environment preparation. If next exclusive task will be added before
360  /// preveous finishes (or even starts) its execution then they will be
361  /// executed consequently each in its own exclusive environment (if flags
362  /// parameter for them is different).
363  ///
364  /// @param task
365  /// Task to execute exclusively
366  /// @param flags
367  /// Parameters of the exclusive environment
368  void RequestExclusiveExecution(CThreadPool_Task* task,
369  TExclusiveFlags flags = 0);
370 
371  /// Cancel the selected groups of tasks in the pool.
372  ///
373  /// @param tasks_group
374  /// Must be a combination of fCancelQueuedTasks and/or
375  /// fCancelExecutingTasks. Cannot be zero.
376  void CancelTasks(TExclusiveFlags tasks_group);
377 
378  /// When to start new threads after flushing old ones
379  ///
380  /// @sa FlushThreads()
381  enum EFlushType {
382  eStartImmediately, ///< New threads can be started immediately
383  eWaitToFinish ///< New threads can be started only when all old
384  ///< threads finished their execution
385  };
386 
387  /// Finish all current threads and replace them with new ones
388  /// @param flush_type
389  /// If new threads can be launched immediately after call to this
390  /// method or only after all "old" threads have been finished.
391  void FlushThreads(EFlushType flush_type);
392 
393  /// Get total number of threads currently running in pool
394  unsigned int GetThreadsCount(void) const;
395 
396  /// Get the number of tasks currently waiting in queue
397  unsigned int GetQueuedTasksCount(void) const;
398 
399  /// Get the number of currently executing tasks
400  unsigned int GetExecutingTasksCount(void) const;
401 
402  /// Does method Abort() was already called for this ThreadPool
403  bool IsAborted(void) const;
404 
405 protected:
406  /// Create new thread for the pool
407  virtual CThreadPool_Thread* CreateThread(void);
408 
409  /// Get the mutex that protects all changes in the pool
410  CMutex& GetMainPoolMutex(void);
411 
412 private:
413  friend class CThreadPool_Impl;
414 
415  /// Prohibit copying and assigning
418 
419  /// Actual implementation of the pool
421 };
422 
423 
424 
425 /// Base class for a thread running inside CThreadPool and executing tasks.
426 ///
427 /// Class can be inherited if it doesn't fit your specific needs. But to use
428 /// inherited class you also will need to inherit CThreadPool and override
429 /// its method CreateThread().
430 
432 {
433 public:
434  /// Get the task currently executing in the thread
435  CRef<CThreadPool_Task> GetCurrentTask(void) const;
436 
437 protected:
438  /// Construct and attach to the pool
440 
441  /// Destructor
442  virtual ~CThreadPool_Thread(void);
443 
444  /// Init this thread. It is called at beginning of Main()
445  virtual void Initialize(void);
446 
447  /// Clean up. It is called by OnExit()
448  virtual void Finalize(void);
449 
450  /// Get the thread pool in which this thread is running
451  CThreadPool* GetPool(void) const;
452 
453 private:
455 
456  /// Prohibit copying and assigning
459 
460  /// To prevent overriding - main thread function
461  virtual void* Main(void);
462 
463  /// To prevent overriding - do cleanup after exiting from thread
464  virtual void OnExit(void);
465 
466  /// Actual implementation of the thread
468 };
469 
470 
471 
472 
473 /// Abstract class for controlling the number of threads in pool.
474 /// Every time when something happens in the pool (new task accepted, task has
475 /// started processing or has been processed, new threads started or some
476 /// threads killed) method HandleEvent() of this class is called. It makes
477 /// some common stuff and then calls OnEvent(). The algorithm in OnEvent()
478 /// has to decide how many threads should be in the pool and call method
479 /// SetThreadsCount() accordingly. For making your own algorithm
480 /// you should inherit this class. You then can pass an instance of the class
481 /// to the ThreadPool's constructor.
482 /// Controller is strictly attached to the pool in the pool's constructor.
483 /// One controller cannot track several ThreadPools and one ThreadPool cannot
484 /// be tracked by several controllers.
485 /// Implementation of this class is threadsafe, so all its parameters can be
486 /// changed during ThreadPool operation.
487 
488 
490 {
491 public:
492  /// Constructor
493  /// @param max_threads
494  /// Maximum number of threads in pool
495  /// @param min_threads
496  /// Minimum number of threads in pool
497  CThreadPool_Controller(unsigned int max_threads,
498  unsigned int min_threads);
499 
500  /// Set the minimum number of threads in pool
501  void SetMinThreads(unsigned int min_threads);
502 
503  /// Get the minimum number of threads in pool
504  unsigned int GetMinThreads(void) const;
505 
506  /// Set the maximum number of threads in pool
507  void SetMaxThreads(unsigned int max_threads);
508 
509  /// Get the maximum number of threads in pool
510  unsigned int GetMaxThreads(void) const;
511 
512  /// Events that can happen with ThreadPool
513  enum EEvent {
514  eSuspend, ///< ThreadPool is suspended for exclusive task execution
515  eResume, ///< ThreadPool is resumed after exclusive task execution
516  eOther ///< All other events (happen asynchronously, so cannot be
517  ///< further distinguished)
518  };
519 
520  /// This method is called every time something happens in a pool,
521  /// such as: new task added, task is started or finished execution,
522  /// new threads started or some threads finished.
523  /// It does the hardcoded must-do processing of the event, and also
524  /// calls OnEvent() callback to run the controlling algorithm.
525  /// Method ensures that OnEvent() always called protected with ThreadPool
526  /// main mutex and that ThreadPool itself is not aborted or in suspended
527  /// for exclusive execution state (except the eSuspend event).
528  ///
529  /// @sa OnEvent()
530  void HandleEvent(EEvent event);
531 
532  /// Get maximum timeout for which calls to method HandleEvent() can be
533  /// missing. Method HandleEvent() will be called after this timeout
534  /// for sure if ThreadPool will not be aborted or in suspended state
535  /// at this moment.
536  virtual CTimeSpan GetSafeSleepTime(void) const;
537 
538 
539 protected:
540  /// Destructor. Have to be called only from CRef
541  virtual ~CThreadPool_Controller(void);
542 
543  /// Main method for the implementation of controlling algorithm.
544  /// Method should not implement any excessive calculations because it
545  /// will be called guarded with main pool mutex and because of that
546  /// it will block several important pool operations.
547  /// @note
548  /// Method will never be called recursively or concurrently in different
549  /// threads (HandleEvent() will take care of this).
550  ///
551  /// @sa HandleEvent()
552  virtual void OnEvent(EEvent event) = 0;
553 
554  /// Get pool to which this class is attached
555  CThreadPool* GetPool(void) const;
556 
557  /// Get mutex which guards access to pool
558  /// All work in controller should be based on the same mutex as in pool.
559  /// So every time when you need to guard access to some members of derived
560  /// class it is recommended to use this very mutex. But NB: it's assumed
561  /// everywhere that this mutex is locked on the small periods of time. So
562  /// be careful and implement the same pattern.
563  CMutex& GetMainPoolMutex(CThreadPool* pool) const;
564 
565  /// Ensure that constraints of minimum and maximum count of threads in pool
566  /// are met. Start new threads or finish overflow threads if needed.
567  void EnsureLimits(void);
568 
569  /// Set number of threads in pool
570  /// Adjust given number to conform to minimum and maximum threads count
571  /// constraints if needed.
572  void SetThreadsCount(unsigned int count);
573 
574 
575 private:
576  friend class CThreadPool_Impl;
577 
578  /// Prohibit copying and assigning
581 
582  /// Attach the controller to ThreadPool
583  void x_AttachToPool(CThreadPool_Impl* pool);
584 
585  /// Detach the controller from pool when pool is aborted
586  void x_DetachFromPool(void);
587 
588  /// ThreadPool to which this controller is attached
589  atomic<CThreadPool_Impl*> m_Pool;
590  /// Minimum number of threads in pool
591  unsigned int m_MinThreads;
592  /// Maximum number of threads in pool
593  unsigned int m_MaxThreads;
594  /// If controller is already inside HandleEvent() processing
596 };
597 
598 
599 
600 
601 /// Exception class for all ThreadPool-related classes
602 
604 {
605 public:
606  enum EErrCode {
607  eControllerBusy, ///< attempt to create several ThreadPools with
608  ///< the same controller
609  eTaskBusy, ///< attempt to change task when it's already placed
610  ///< into ThreadPool or to put task in ThreadPool
611  ///< several times
612  eProhibited, ///< attempt to do something when ThreadPool was
613  ///< already aborted or to add task when it is
614  ///< prohibited by flags of exclusive execution
615  eInactive, ///< attempt to call active methods in
616  ///< ThreadPool_Controller when it is not attached
617  ///< to any ThreadPool
618  eInvalid ///< attempt to operate task added in one ThreadPool
619  ///< by means of methods of another ThreadPool or
620  ///< invalid parameters in the constructor
621  };
622  virtual const char* GetErrCodeString(void) const override;
624 };
625 
626 
627 
628 //////////////////////////////////////////////////////////////////////////
629 // All inline methods
630 //////////////////////////////////////////////////////////////////////////
631 
632 inline bool
634 {
635  return m_CancelRequested;
636 }
637 
640 {
641  return m_Status;
642 }
643 
644 inline bool
646 {
647  return m_Status >= eCompleted;
648 }
649 
650 inline unsigned int
652 {
653  return m_Priority;
654 }
655 
656 
657 inline unsigned int
659 {
660  return m_MinThreads;
661 }
662 
663 inline unsigned int
665 {
666  return m_MaxThreads;
667 }
668 
669 
671 
672 
673 /* @} */
674 
675 #endif /* UTIL__THREAD_POOL__HPP */
CAtomicCounter_WithAutoInit –.
Definition: ncbicntr.hpp:120
CMutex –.
Definition: ncbimtx.hpp:749
CObject –.
Definition: ncbiobj.hpp:180
Exception class for all ThreadPool-related classes.
Abstract class for controlling the number of threads in pool.
Real implementation of all ThreadPool functions.
Definition: thread_pool.cpp:59
void x_Init(CThreadPool *pool_intf, CThreadPool_Controller *controller, CThread::TRunMode threads_mode)
Initialization of all class member variables that can be initialized outside of constructor.
Abstract class for representing single task executing in pool of threads To use this class in applica...
Definition: thread_pool.hpp:76
Real implementation of all CThreadPool_Thread functions.
void OnExit(void)
Implementation of threadOnExit() method.
Base class for a thread running inside CThreadPool and executing tasks.
Main class implementing functionality of pool of threads.
CTimeSpan.
Definition: ncbitime.hpp:1313
static uch flags
int Main(int argc, const char *argv[])
#define NULL
Definition: ncbistd.hpp:225
NCBI_XNCBI_EXPORT void Abort(void)
Smart abort function.
Definition: ncbidiag.cpp:8147
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
Definition: ncbiobj.hpp:482
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
virtual void OnEvent(EEvent event)=0
Main method for the implementation of controlling algorithm.
atomic< CThreadPool_Impl * > m_Pool
ThreadPool to which this controller is attached.
EEvent
Events that can happen with ThreadPool.
atomic< EStatus > m_Status
Status of the task.
unsigned int m_Priority
Priority of the task.
virtual EStatus Execute(void)=0
Do the actual job.
unsigned int GetMinThreads(void) const
Get the minimum number of threads in pool.
atomic< CThreadPool_Impl * > m_Pool
Pool owning this task.
EStatus GetStatus(void) const
Get status of the task.
atomic< bool > m_CancelRequested
Flag indicating if cancellation of the task was already requested.
unsigned int GetPriority(void) const
Get priority of the task.
unsigned int m_MaxThreads
Maximum number of threads in pool.
unsigned int GetMaxThreads(void) const
Get the maximum number of threads in pool.
EStatus
Status of the task.
Definition: thread_pool.hpp:79
CThreadPool_Thread(const CThreadPool_Thread &)
Prohibit copying and assigning.
CThreadPool_Impl * m_Impl
Actual implementation of the pool.
CThreadPool_ThreadImpl * m_Impl
Actual implementation of the thread.
EFlushType
When to start new threads after flushing old ones.
unsigned int TExclusiveFlags
Type of bit-masked combination of several values from EExclusiveFlags.
NCBI_EXCEPTION_DEFAULT(CThreadPoolException, CException)
bool IsFinished(void) const
Check if task execution has been already finished (successfully or not)
CAtomicCounter_WithAutoInit m_IsBusy
Flag indicating that the task is already added to some pool.
bool IsCancelRequested(void) const
Check if cancellation of the task was requested.
CThreadPool_Controller(const CThreadPool_Controller &)
Prohibit copying and assigning.
bool m_InHandleEvent
If controller is already inside HandleEvent() processing.
EExclusiveFlags
Binary flags indicating different possible options in what environment the pool will execute exclusiv...
CThreadPool(const CThreadPool &)
Prohibit copying and assigning.
unsigned int m_MinThreads
Minimum number of threads in pool.
@ eResume
ThreadPool is resumed after exclusive task execution.
@ eSuspend
ThreadPool is suspended for exclusive task execution.
@ eIdle
has not been placed in queue yet
Definition: thread_pool.hpp:80
@ eQueued
in the queue, awaiting execution
Definition: thread_pool.hpp:81
@ eExecuting
being executed
Definition: thread_pool.hpp:82
@ eFailed
failure during execution
Definition: thread_pool.hpp:84
@ eCompleted
executed successfully
Definition: thread_pool.hpp:83
@ eStartImmediately
New threads can be started immediately.
int TRunMode
Bitwise OR'd flags for thread creation passed to Run().
Definition: ncbithr.hpp:558
@ fRunDefault
Default mode.
Definition: ncbithr.hpp:540
@ eCanceled
Request canceled.
NCBI_XUTIL_EXPORT
Parameter to control printing diagnostic message about conversion of static array data from a differe...
Definition: static_set.hpp:72
Modified on Mon Feb 26 04:01:47 2024 by modify_doxy.py rev. 669887