38 #define NCBI_USE_ERRCODE_X Util_Thread
90 unsigned int queue_size,
91 unsigned int max_threads,
92 unsigned int min_threads,
101 unsigned int queue_size,
535 virtual void*
Main(
void);
615 <<
"Wrong status returned from "
616 "CThreadPool_Task::Execute(): "
805 bool has_no_threads =
false;
811 if (has_no_threads) {
927 "Cannot attach Controller to several ThreadPools.");
970 "Cannot change task when it is already added "
996 "Cannot add task in ThreadPool several times");
1017 if (old_status != new_status && old_status !=
eCanceled) {
1219 : m_Interface(thread_intf),
1222 m_CancelRequested(
false),
1367 catch (exception& e) {
1368 ERR_POST_X(7,
"Exception from task in ThreadPool: " << e);
1374 ERR_POST_X(7,
"Non-standard exception from task in ThreadPool");
1405 if (queue_size == 0) {
1420 unsigned int queue_size,
1421 unsigned int max_threads,
1422 unsigned int min_threads,
1424 : m_Queue(x_GetQueueSize(queue_size)),
1435 unsigned int queue_size,
1438 : m_Queue(x_GetQueueSize(queue_size)),
1442 x_Init(pool_intf, controller, threads_mode);
1506 for (
unsigned int i = 0;
i <
count; ++
i) {
1536 (*it)->RequestToFinish();
1548 (*it)->RequestToFinish();
1576 if (it != to_del->
end()) {
1629 bool done = (this->*wait_func)();
1651 pool_guard->
Guard();
1652 done = (this->*wait_func)();
1665 "Adding of new tasks is prohibited");
1682 unique_ptr<CTimeSpan> adjusted_timeout;
1692 "Cannot add task - all threads are busy");
1702 timeout = adjusted_timeout.get();
1746 if (! (*it)->IsFinishing()) {
1764 while (it != q_guard.
End() && *it != task) {
1768 if (it != q_guard.
End()) {
1785 "Cannot add exclusive task when ThreadPool is aborted");
1820 "Cannot cancel task execution "
1821 "if it is inserted in another ThreadPool");
1836 && tasks_group != 0);
1854 (*it)->CancelCurrentTask();
1861 (*it)->CancelCurrentTask();
1871 it != q_guard.
End(); ++it)
1873 it->GetNCPointer()->x_RequestToCancel();
1886 "Cannot flush threads when ThreadPool aborted");
1895 bool need_add =
true;
1926 if ( n_queued_tasks ) {
1928 "CThreadPool is being aborted or destroyed while still "
1929 "having " << n_queued_tasks <<
" regular tasks "
1930 "waiting to be executed; they are now canceled");
1942 if ( n_exclusive_tasks ) {
1944 "CThreadPool is being aborted or destroyed while still "
1945 "having " << n_exclusive_tasks <<
" exclusive tasks "
1946 "waiting to be executed; they are now canceled");
1950 it != q_guard.
End(); ++it)
1952 it->task->x_RequestToCancel();
1976 "CThreadPool::Abort() was unable to terminate "
1977 "all of its threads within the specified timeout: "
1981 "CThreadPool::Abort() was not able to terminate"
1982 "all of its threads despite being given an infinite "
1983 "time for doing so");
1996 unsigned int min_threads)
1998 m_MinThreads(min_threads),
2000 m_InHandleEvent(
false)
2002 if (max_threads < min_threads || max_threads == 0) {
2004 "Invalid numbers of min and max number of threads:"
2005 " min=" << min_threads <<
", max=" << max_threads);
2026 "Cannot do active work when not attached "
2027 "to some ThreadPool");
2029 return impl->GetMainPoolMutex();
2084 if (
count > now_cnt) {
2087 else if (
count < now_cnt) {
2178 unsigned int max_threads,
2179 unsigned int min_threads,
void Guard(resource_type &resource)
Manually force the guard to protect some other resource.
void Release()
Manually force the resource to be released.
Exception object used throughout all CSyncQueue classes.
Access guard to non-constant CSyncQueue.
TIterator End(void)
Get iterator pointing to the tail of the queue.
TIterator Begin(void)
Get iterator pointing to the head of the queue.
TIterator Erase(TIterator iter)
Erase one element in the queue.
Iterator for CSyncQueue (constant or non-constant depending on template parameters).
Adaptor class to use STL multiset<> in CSyncQueue.
Thread-safe queue object with a blocking mechanism.
TSize GetSize(void) const
Get count of elements already stored in the queue.
Container::size_type TSize
Type of size of the queue.
void Push(const TValue &elem, const CTimeSpan *timeout=NULL)
Add new element to the end of queue.
TValue Pop(const CTimeSpan *timeout=NULL)
Retrieve an element from the queue.
void Clear(const CTimeSpan *timeout=NULL)
Remove all elements from the queue.
Exception class for all ThreadPool-related classes.
Default ThreadPool controller based on Proportional-Integral-Derivative algorithm.
Abstract class for controlling the number of threads in pool.
Special task which does nothing It's used in FlushThreads to force pool to wait while all old threads...
CThreadPool_EmptyTask(void)
virtual EStatus Execute(void)
Empty main method.
Guardian for protecting pool by locking its main mutex.
CThreadPool_Guard(CThreadPool_Impl *pool, bool is_active=true)
Constructor.
CThreadPool_Impl * m_Pool
Pool protected by the guardian.
void Release(void)
Turn this guardian off.
void Guard(void)
Turn this guardian on.
Real implementation of all ThreadPool functions.
void TaskStarting(void)
Callback from thread when it is starting to execute task.
TExclusiveQueue m_ExclusiveQueue
Queue for information about exclusive tasks.
void FlushThreads(CThreadPool::EFlushType flush_type)
Finish all current threads and replace them with new ones.
CRef< CThreadPool_Impl > m_SelfRef
Reference to this pool to prevent its destroying earlier than we allow it to.
SExclusiveTaskInfo TryGetExclusiveTask(void)
Get the next exclusive task to execute.
void x_RemoveTaskFromQueue(const CThreadPool_Task *task)
Delete task from the queue If task does not exist in queue then does nothing.
void AddTask(CThreadPool_Task *task, const CTimeSpan *timeout)
Add task to the pool.
static CThreadPool_Impl * s_GetImplPointer(CThreadPool *pool)
Convert pointer to CThreadPool object into pointer to CThreadPool_Impl object.
CThreadPool_Impl(const CThreadPool_Impl &)
Prohibit copying and assigning.
static void sx_SetTaskStatus(CThreadPool_Task *task, CThreadPool_Task::EStatus status)
Call x_SetTaskStatus() for the given task.
volatile TExclusiveFlags m_SuspendFlags
Requested requirements for the exclusive execution environment.
CRef< CThreadPool_ServiceThread > m_ServiceThread
Thread for execution of exclusive tasks and passing of events to the controller.
CThread::TRunMode m_ThreadsMode
Running mode of all threads.
void RequestExclusiveExecution(CThreadPool_Task *task, TExclusiveFlags flags)
Add the task for exclusive execution in the pool.
bool(CThreadPool_Impl::* TWaitPredicate)(void) const
Type of some simple predicate.
CAtomicCounter m_ThreadsCount
Total number of threads Introduced for more adequate and fast reflecting to threads starting and stop...
void x_CancelExecutingTasks(void)
Cancel all currently executing tasks.
void DestroyReference(void)
Destroy reference to this object Method is called when CThreadPool object is destroyed which means th...
const CTimeSpan & GetDestroyTimeout(void) const
Get destroy timeout for the pool.
CThreadPool_Impl & operator=(const CThreadPool_Impl &)
unsigned int GetQueuedTasksCount(void) const
Get the number of tasks currently waiting in queue.
void CallController(CThreadPool_Controller::EEvent event)
Call the CThreadPool_Controller::HandleEvent() method of the pool controller with the given event typ...
TThreadsList m_IdleThreads
List of all idle threads.
CSemaphore m_RoomWait
Semaphore for waiting for available threads to process task when queuing is disabled.
CMutex & GetMainPoolMutex(void)
Get main pool mutex.
CTimeSpan GetSafeSleepTime(void) const
Call the CThreadPool_Controller::GetSafeSleepTime() method of the pool controller.
bool CanDoExclusiveTask(void) const
Check if it is already allowed to execute exclusive task.
void ThreadStopped(CThreadPool_ThreadImpl *thread)
Callback from working thread when it finished its Main() method.
atomic< bool > m_Suspended
If pool is suspended for exclusive task execution or not.
void LaunchThreads(unsigned int count)
Launch new threads in pool.
void ResumeWork(void)
Resume the pool operation after exclusive task execution.
TQueue m_Queue
Queue for storing tasks.
bool m_IsQueueAllowed
Flag about working with special case: FALSE - queue_size == 0, TRUE - queue_size > 0.
CThreadPool * m_Interface
ThreadPool interface object attached to this implementation.
void SetInterfaceStarted(void)
Mark that initialization of the interface was finished.
volatile bool m_FlushRequested
Flag indicating if flush of threads requested after adding exclusive task but before it is started it...
bool x_HasNoThreads(void) const
Check if all threads in pool finished their work.
void Abort(const CTimeSpan *timeout)
Abort the pool operation.
void CancelTasks(TExclusiveFlags tasks_group)
Cancel the selected groups of tasks in the pool.
CRef< CThreadPool_Controller > m_Controller
Controller managing count of threads in pool.
CThreadPool * GetPoolInterface(void) const
Get pointer to ThreadPool interface object.
unsigned int GetThreadsCount(void) const
Get number of threads running in the pool.
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.
bool x_CanAddImmediateTask(void) const
Check if new task can be added to the pool when queueiing is disabled.
CAtomicCounter m_TotalTasks
Total number of tasks acquired by pool Includes queued tasks and executing (but not exclusive!...
void FinishThreads(unsigned int count)
Finish threads in pool Stop first all idle threads then stop busy threads without cancelation of curr...
void TaskFinished(void)
Callback from thread when it has finished to execute task.
CThreadPool_Impl(CThreadPool *pool_intf, unsigned int queue_size, unsigned int max_threads, unsigned int min_threads, CThread::TRunMode threads_mode=CThread::fRunDefault)
Constructor with default controller.
CSemaphore m_AbortWait
Semaphore for waiting for threads finishing in Abort() method.
set< CThreadPool_ThreadImpl * > TThreadsList
Type of list of all poolled threads.
void CancelTask(CThreadPool_Task *task)
Request to cancel the task.
bool IsAborted(void) const
Check if the pool is already aborted.
void x_CancelQueuedTasks(void)
Cancel all tasks waiting in the queue.
void SetDestroyTimeout(const CTimeSpan &timeout)
Set destroy timeout for the pool.
~CThreadPool_Impl(void)
Destructor. Will be called from CRef.
static void sx_RequestToCancel(CThreadPool_Task *task)
Call x_RequestToCancel() for the given task.
bool IsSuspended(void) const
Check if the pool is suspended for exclusive execution.
TThreadsList m_WorkingThreads
List of all threads currently executing some tasks.
CAtomicCounter m_ExecutingTasks
Number of tasks executing now Introduced for more adequate and fast reflecting to task executing star...
void RequestSuspend(TExclusiveFlags flags)
Request suspension of the pool.
unsigned int GetExecutingTasksCount(void) const
Get the number of currently executing tasks.
bool SetThreadIdle(CThreadPool_ThreadImpl *thread, bool is_idle)
Mark thread as idle or non-idle.
CThreadPool::TExclusiveFlags TExclusiveFlags
void CallControllerOther(void)
Schedule running of CThreadPool_Controller::HandleEvent() with eOther event type.
void ThreadStateChanged(void)
Callback when some thread changed its idleness or finished (including service thread)
CTimeSpan m_DestroyTimeout
Timeout to wait for all threads to finish before the ThreadPool interface object will be able to dest...
CRef< CThreadPool_Task > TryGetNextTask(void)
Get next task from queue if there is one If the queue is empty then return NULL.
CSyncQueue< SExclusiveTaskInfo > TExclusiveQueue
Type of queue used for storing information about exclusive tasks.
CSyncQueue< CRef< CThreadPool_Task >, CSyncQueue_multiset< CRef< CThreadPool_Task >, SThreadPool_TaskCompare > > TQueue
Type of queue used for storing tasks.
atomic< bool > m_Aborted
If pool is already aborted or not.
unsigned int x_GetQueueSize(unsigned int queue_size)
Transform size of queue given in constructor to the size passed to CSyncQueue constructor.
bool x_WaitForPredicate(TWaitPredicate wait_func, CThreadPool_Guard *pool_guard, CSemaphore *wait_sema, const CTimeSpan *timeout, const CStopWatch *timer)
Wait for some predicate to be true.
bool x_NoNewTaskAllowed(void) const
Check if addeding new tasks to the pool is prohibited.
CMutex m_MainPoolMutex
Mutex for guarding all changes in the pool, its threads and controller.
Thread used in pool for different internal needs: execution of exclusive tasks and passing events to ...
bool IsFinished(void)
Check if this thread have already finished or not.
CRef< CThreadPool_Impl > m_Pool
Pool owning this thread.
void NeedCallController(void)
Tell the thread that controller should handle eOther event.
CSemaphore m_IdleTrigger
Semaphore for idle sleeping.
void WakeUp(void)
Wake up from idle waiting or waiting of pool preparing exclusive environment.
virtual void * Main(void)
Main thread execution.
virtual ~CThreadPool_ServiceThread(void)
Destructor. Will be called from CRef.
CThreadPool_ServiceThread(CThreadPool_Impl *pool)
Constructor.
atomic< bool > m_Finished
If the thread has already finished its Main() method.
CRef< CThreadPool_Task > m_CurrentTask
Currently executing exclusive task.
atomic< bool > m_Finishing
If finishing of the thread is already requested.
CAtomicCounter m_NeedCallController
Flag indicating that thread should pass eOther event to the controller.
void RequestToFinish(void)
Request finishing of the thread.
CFastMutex m_FastMutex
General-use mutex for very (very!) trivial ops.
void x_Idle(void)
Do "idle" work when thread is not busy executing exclusive tasks.
Abstract class for representing single task executing in pool of threads To use this class in applica...
Real implementation of all CThreadPool_Thread functions.
CThreadPool * GetPool(void) const
Get ThreadPool interface object owning this thread.
CRef< CThreadPool_Task > m_CurrentTask
Task currently executing in the thread.
static CThreadPool_ThreadImpl * s_GetImplPointer(CThreadPool_Thread *thread)
Convert pointer to CThreadPool_Thread object into pointer to CThreadPool_ThreadImpl object.
void RequestToFinish(void)
Request this thread to finish its operation.
~CThreadPool_ThreadImpl(void)
Destructor Called directly from CThreadPool destructor.
CThreadPool_ThreadImpl & operator=(const CThreadPool_ThreadImpl &)
CThreadPool_ThreadImpl(CThreadPool_Thread *thread_intf, CThreadPool_Impl *pool)
Constructor.
bool IsFinishing(void) const
If finishing of this thread is already in progress or not.
void x_TaskFinished(CThreadPool_Task::EStatus status)
Do finalizing when task finished its execution.
CSemaphore m_IdleTrigger
Semaphore for waking up from idle waiting.
void WakeUp(void)
Wake up the thread from idle state.
CThreadPool_ThreadImpl(const CThreadPool_ThreadImpl &)
Prohibit copying and assigning.
bool x_SetIdleState(bool is_idle)
Mark the thread idle or non-idle.
atomic< bool > m_Finishing
If the thread is already asked to finish or not.
CFastMutex m_FastMutex
General-use mutex for very (very!) trivial ops.
void x_Idle(void)
Suspend until the wake up signal.
bool m_IsIdle
Idleness of the thread.
void Main(void)
Implementation of thread Main() method.
CRef< CThreadPool_Impl > m_Pool
Pool running the thread.
atomic< bool > m_CancelRequested
If cancel of the currently executing task is requested or not.
void CancelCurrentTask(void)
Request to cancel current task execution.
static CThreadPool_Thread * s_CreateThread(CThreadPool *pool)
Create new CThreadPool_Thread object Method introduced to avoid excessive friendship to CThreadPool_T...
CRef< CThreadPool_Task > GetCurrentTask(void) const
Get task currently executing in the thread May be NULL if thread is idle or is in the middle of chang...
CThreadPool_Thread * m_Interface
ThreadPool_Thread interface object attached to this implementation.
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.
CTimeout – Timeout interval.
iterator_bool insert(const value_type &val)
parent_type::iterator iterator
const_iterator find(const key_type &key) const
const_iterator end() const
static void check_flags(TDSCOLUMN *curcol, int n, const char *possible_results)
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
#define REVERSE_ITERATE(Type, Var, Cont)
ITERATE macro to reverse sequence through container elements.
@ eNegative
Value is negative.
TNCBIAtomicValue TValue
Alias TValue for TNCBIAtomicValue.
void Set(TValue new_value) THROWS_NONE
Set atomic counter value.
TValue Add(int delta) THROWS_NONE
Atomically add value (=delta), and return new counter value.
TValue Get(void) const THROWS_NONE
Get atomic counter value.
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
void Critical(CExceptionArgs_Base &args)
#define NCBI_CATCH_ALL_X(err_subcode, message)
#define STD_CATCH_ALL_X(err_subcode, message)
Standard handling of "exception"-derived exceptions; catches non-standard exceptions and generates "u...
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
void Warning(CExceptionArgs_Base &args)
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
TObjectType * GetNCPointerOrNull(void) const THROWS_NONE
Get pointer value.
bool NotNull(void) const THROWS_NONE
Check if pointer is not null – same effect as NotEmpty().
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
void Reset(void)
Reset reference object.
bool NotEmpty(void) const THROWS_NONE
Check if CRef is not empty – pointing to an object and has a non-null value.
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
bool IsNull(void) const THROWS_NONE
Check if pointer is null – same effect as Empty().
uint32_t Uint4
4-byte (32-bit) unsigned integer
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
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.
friend class CThreadPool_ThreadImpl
EEvent
Events that can happen with ThreadPool.
CThreadPool(unsigned int queue_size, unsigned int max_threads, unsigned int min_threads=2, CThread::TRunMode threads_mode=CThread::fRunDefault)
Constructor.
atomic< EStatus > m_Status
Status of the task.
void x_AttachToPool(CThreadPool_Impl *pool)
Attach the controller to ThreadPool.
void EnsureLimits(void)
Ensure that constraints of minimum and maximum count of threads in pool are met.
unsigned int m_Priority
Priority of the task.
virtual void OnStatusChange(EStatus old)
Callback to notify on changes in the task status.
virtual EStatus Execute(void)=0
Do the actual job.
void CancelTask(CThreadPool_Task *task)
Request to cancel the task and remove it from queue if it is there.
virtual void OnExit(void)
To prevent overriding - do cleanup after exiting from thread.
unsigned int GetMinThreads(void) const
Get the minimum number of threads in pool.
CThreadPool * GetPool(void) const
Get the thread pool in which this thread is running.
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.
CThreadPool * GetPool(void) const
Get pool to which this class is attached.
void x_Init(unsigned int priority)
Init all members in constructor.
unsigned int GetPriority(void) const
Get priority of the task.
virtual CThreadPool_Thread * CreateThread(void)
Create new thread for the pool.
virtual CTimeSpan GetSafeSleepTime(void) const
Get maximum timeout for which calls to method HandleEvent() can be missing.
virtual void OnCancelRequested(void)
Callback to notify when cancellation of the task is requested.
CThreadPool_Controller(unsigned int max_threads, unsigned int min_threads)
Constructor.
CRef< CThreadPool_Task > GetCurrentTask(void) const
Get the task currently executing in the thread.
void x_SetOwner(CThreadPool_Impl *pool)
Set pool as owner of this task.
unsigned int GetThreadsCount(void) const
Get total number of threads currently running in pool.
unsigned int m_MaxThreads
Maximum number of threads in pool.
CThreadPool * GetPool(void) const
The thread pool which accepted this task for execution.
CMutex & GetMainPoolMutex(CThreadPool *pool) const
Get mutex which guards access to pool All work in controller should be based on the same mutex as in ...
virtual void Initialize(void)
Init this thread. It is called at beginning of Main()
void x_RequestToCancel(void)
Internal canceling of the task.
void x_ResetOwner(void)
Detach task from the pool (if insertion into the pool has failed).
void Abort(const CTimeSpan *timeout=NULL)
Abort all functions of the pool – cancel all queued tasks, send cancellation notifications to all cur...
void FlushThreads(EFlushType flush_type)
Finish all current threads and replace them with new ones.
unsigned int GetMaxThreads(void) const
Get the maximum number of threads in pool.
CThreadPool_Task(unsigned int priority=0)
Constructor.
EStatus
Status of the task.
virtual ~CThreadPool(void)
Destructor – will wait for all its threads to finish with the timeout set by CThreadPool::SetDestroyT...
CThreadPool_Impl * m_Impl
Actual implementation of the pool.
CThreadPool_ThreadImpl * m_Impl
Actual implementation of the thread.
virtual ~CThreadPool_Controller(void)
Destructor. Have to be called only from CRef.
void x_DetachFromPool(void)
Detach the controller from pool when pool is aborted.
EFlushType
When to start new threads after flushing old ones.
bool IsAborted(void) const
Does method Abort() was already called for this ThreadPool.
void HandleEvent(EEvent event)
This method is called every time something happens in a pool, such as: new task added,...
void RequestExclusiveExecution(CThreadPool_Task *task, TExclusiveFlags flags=0)
Add the task for exclusive execution in the pool By default the pool suspends all new and queued task...
CMutex & GetMainPoolMutex(void)
Get the mutex that protects all changes in the pool.
const CTimeSpan & GetDestroyTimeout(void) const
Get timeout to wait for all threads to finish before the pool will be able to destroy.
unsigned int TExclusiveFlags
Type of bit-masked combination of several values from EExclusiveFlags.
virtual ~CThreadPool_Thread(void)
Destructor.
void SetMinThreads(unsigned int min_threads)
Set the minimum number of threads in pool.
bool IsFinished(void) const
Check if task execution has been already finished (successfully or not)
void RequestToCancel(void)
Cancel the task.
void CancelTasks(TExclusiveFlags tasks_group)
Cancel the selected groups of tasks in the pool.
CAtomicCounter_WithAutoInit m_IsBusy
Flag indicating that the task is already added to some pool.
void SetThreadsCount(unsigned int count)
Set number of threads in pool Adjust given number to conform to minimum and maximum threads count con...
void SetDestroyTimeout(const CTimeSpan &timeout)
Set timeout to wait for all threads to finish before the pool should be able to destroy.
virtual void Finalize(void)
Clean up. It is called by OnExit()
CThreadPool_Task & operator=(const CThreadPool_Task &other)
Assignment.
virtual ~CThreadPool_Task(void)
Destructor. Will be called from CRef.
unsigned int GetExecutingTasksCount(void) const
Get the number of currently executing tasks.
bool IsCancelRequested(void) const
Check if cancellation of the task was requested.
virtual void * Main(void)
To prevent overriding - main thread function.
void x_SetStatus(EStatus new_status)
Set task status.
bool m_InHandleEvent
If controller is already inside HandleEvent() processing.
void SetMaxThreads(unsigned int max_threads)
Set the maximum number of threads in pool.
void AddTask(CThreadPool_Task *task, const CTimeSpan *timeout=NULL)
Add task to the pool for execution.
unsigned int GetQueuedTasksCount(void) const
Get the number of tasks currently waiting in queue.
CThreadPool_Thread(CThreadPool *pool)
Construct and attach to the pool.
unsigned int m_MinThreads
Minimum number of threads in pool.
friend class CThreadPool_Impl
@ eOther
All other events (happen asynchronously, so cannot be further distinguished)
@ eResume
ThreadPool is resumed after exclusive task execution.
@ eSuspend
ThreadPool is suspended for exclusive task execution.
@ eIdle
has not been placed in queue yet
@ eQueued
in the queue, awaiting execution
@ eExecuting
being executed
@ eFailed
failure during execution
@ eCompleted
executed successfully
@ eCanceled
canceled - possible only if canceled before processing was started or if method Execute() returns res...
@ eWaitToFinish
New threads can be started only when all old threads finished their execution.
@ eStartImmediately
New threads can be started immediately.
@ fCancelQueuedTasks
Cancel all tasks waiting in the queue and not yet executing.
@ fCancelExecutingTasks
Cancel all currently executing tasks.
@ fExecuteQueuedTasks
Execute all tasks waiting in the queue before execution of exclusive task.
@ fDoNotAllowNewTasks
Do not allow to add new tasks to the pool during exclusive task execution.
@ fFlushThreads
Finish all threads currently running in the pool.
unsigned int m_MaxThreads
Maximum simultaneous threads.
bool Run(TRunMode flags=fRunDefault)
Run the thread.
void Wait(void)
Wait on semaphore.
int TRunMode
Bitwise OR'd flags for thread creation passed to Run().
bool TryWait(unsigned int timeout_sec=0, unsigned int timeout_nsec=0)
Timed wait.
void Post(unsigned int count=1)
Increment the semaphore by "count".
@ fRunAllowST
Allow threads to run in single thread builds.
@ fRunDefault
Default mode.
@ fRunDetached
Run the thread detached (non-joinable)
double Elapsed(void) const
Return time elapsed since first Start() or last Restart() call (in seconds).
long GetNanoSecondsAfterSecond(void) const
Get number of nanoseconds.
double GetAsDouble(void) const
Return time span as number of seconds.
ESign GetSign(void) const
Get sign of time span.
string AsSmartString(ESmartStringPrecision precision, ERound rounding, ESmartStringZeroMode zero_mode=eSSZ_SkipZero) const
Transform time span to "smart" string.
long GetCompleteSeconds(void) const
Get number of complete seconds.
@ eStart
Start timer immediately after creating.
unsigned int
A callback function used to compare two keys in a database.
Definition of all error codes used in util (xutil.lib).
Type for storing information about exclusive task launching.
CRef< CThreadPool_Task > task
SExclusiveTaskInfo(TExclusiveFlags f, CRef< CThreadPool_Task > t)
Functor to compare tasks by priority.
bool operator()(const CRef< CThreadPool_Task > &left, const CRef< CThreadPool_Task > &right) const
Definition of synchronized queue (CSyncQueue template) and templates related to it.
static CThreadPool_Task::EStatus s_ConvertTaskResult(CThreadPool_Task::EStatus status)
Check if status returned from CThreadPool_Task::Execute() is allowed and change it to eCompleted valu...
const CAtomicCounter::TValue kNeedCallController_Shift
static void ThrowAddProhibited(void)
Throw an exception with standard message when AddTask() is called but ThreadPool is aborted or do not...
Pool of generic task-executing threads.
Implementations of controllers for ThreadPool.