AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AThreadPool Class Reference

Thread pool implementation. More...

#include <AUI/Thread/AThreadPool.h>

Classes

class  TryLaterException
 
class  Worker
 

Public Types

enum  Priority { PRIORITY_HIGHEST , PRIORITY_MEDIUM , PRIORITY_LOWEST }
 

Public Member Functions

 AThreadPool (size_t size)
 Initializes the thread pool with size of threads.
 
 AThreadPool ()
 Initializes the thread pool with max(std::thread::hardware_concurrency() - 1, 2) of threads or –aui-threadpool-size=SIZE passed to your application.
 
size_t getPendingTaskCount ()
 
void run (const std::function< void()> &fun, Priority priority=PRIORITY_MEDIUM)
 
void clear ()
 
void runLaterTasks ()
 
void setWorkersCount (std::size_t workersCount)
 
void wakeUpAll ()
 
size_t getTotalWorkerCount () const
 
size_t getIdleWorkerCount () const
 
template<typename Iterator, typename Functor>
auto parallel (Iterator begin, Iterator end, Functor &&functor)
 
template<aui::invocable Callable>
auto operator* (Callable fun)
 

Static Public Member Functions

static void enqueue (const std::function< void()> &fun, Priority priority=PRIORITY_MEDIUM)
 
static AThreadPoolglobal ()
 Global thread pool created with the default constructor.
 

Protected Types

typedef std::function< void()> task
 

Protected Attributes

AVector< _< Worker > > mWorkers
 
AQueue< task > mQueueHighest
 
AQueue< task > mQueueMedium
 
AQueue< task > mQueueLowest
 
AQueue< task > mQueueTryLater
 
std::mutex mQueueLock
 
std::condition_variable mCV
 
size_t mIdleWorkers = 0
 

Detailed Description

Thread pool implementation.

See also
AThreadPool::global()

Constructor & Destructor Documentation

◆ AThreadPool()

AThreadPool::AThreadPool ( size_t size)

Initializes the thread pool with size of threads.

Parameters
sizethread count to initialize.

Member Function Documentation

◆ parallel()

template<typename Iterator, typename Functor>
auto AThreadPool::parallel ( Iterator begin,
Iterator end,
Functor && functor )

Parallels work of some range, grouping tasks per thread (i.e. for 8 items on a 4-core processor each core will process 2 items)

Parameters
beginrange begin
endrange end
functora functor of the following signature:
Result(Iterator begin, Iterator end)
Returns
future set per thread (i.e. for 8 items on a 4-core processor there will be 4 futures)
Performance note
When this function is used to write to the source data it would not be L1-cache friendly. Consider writing results to another location.

The documentation for this class was generated from the following files:
Collaboration diagram for AThreadPool: