FFmpegfs Fuse Multi Media Filesystem 2.16
Public Types | Public Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | List of all members
thread_pool Class Reference

The thread_pool class. More...

#include <thread_pool.h>

Public Types

typedef std::function< int(void)> FunctionPointer
 Pointer to thread pool function. More...
 

Public Member Functions

 thread_pool (unsigned int num_threads=std::thread::hardware_concurrency() *4)
 Construct a thread_pool object. More...
 
virtual ~thread_pool ()
 Object destructor. Ends all threads and cleans up resources. More...
 
int init (unsigned int num_threads=0)
 Initialise thread pool. Initialise the thread pool. Does nothing if called more than once. More...
 
void tear_down (bool silent=false)
 Shut down the thread pool. More...
 
bool schedule_thread (FunctionPointer &&func)
 Schedule a new thread from pool. More...
 
unsigned int current_running () const
 Get number of currently running threads. More...
 
unsigned int current_queued ()
 Get number of currently queued threads. More...
 
unsigned int pool_size () const
 Get current pool size. More...
 

Protected Attributes

std::vector< std::thread > m_thread_pool
 
std::mutex m_queue_mutex
 
std::condition_variable m_queue_cond
 
std::queue< FunctionPointerm_thread_queue
 
std::atomic_bool m_queue_shutdown
 
unsigned int m_num_threads
 
unsigned int m_cur_threads
 
std::atomic_uint32_t m_threads_running
 

Private Member Functions

void loop_function ()
 Start loop function. More...
 

Static Private Member Functions

static void loop_function_starter (thread_pool &tp)
 Start loop function. More...
 

Detailed Description

The thread_pool class.

Definition at line 54 of file thread_pool.h.

Member Typedef Documentation

◆ FunctionPointer

typedef std::function<int(void)> thread_pool::FunctionPointer

Pointer to thread pool function.

Definition at line 57 of file thread_pool.h.

Constructor & Destructor Documentation

◆ thread_pool()

thread_pool::thread_pool ( unsigned int  num_threads = std::thread::hardware_concurrency() * 4)
explicit

Construct a thread_pool object.

Parameters
[in]num_threads- Optional: number of threads to create in pool. Defaults to Defaults to 4 x number of CPU cores.

Definition at line 36 of file thread_pool.cc.

◆ ~thread_pool()

thread_pool::~thread_pool ( )
virtual

Object destructor. Ends all threads and cleans up resources.

Definition at line 44 of file thread_pool.cc.

References tear_down().

Member Function Documentation

◆ current_queued()

unsigned int thread_pool::current_queued ( )

Get number of currently queued threads.

Returns
Returns number of currently queued threads.

Definition at line 109 of file thread_pool.cc.

References m_queue_mutex, and m_thread_queue.

◆ current_running()

unsigned int thread_pool::current_running ( ) const

Get number of currently running threads.

Returns
Returns number of currently running threads.

Definition at line 104 of file thread_pool.cc.

References m_threads_running.

◆ init()

int thread_pool::init ( unsigned int  num_threads = 0)

Initialise thread pool. Initialise the thread pool. Does nothing if called more than once.

Parameters
[in]num_threads- Optional: number of threads to create in pool. Defaults to Defaults to 4x number of CPU cores.
Returns
Number of threads created on success; on error or if called more than once, returns 0.

Definition at line 121 of file thread_pool.cc.

References Logging::info(), loop_function_starter(), m_num_threads, m_thread_pool, and Logging::warning().

◆ loop_function()

void thread_pool::loop_function ( )
private

Start loop function.

Definition at line 54 of file thread_pool.cc.

References m_cur_threads, m_queue_cond, m_queue_mutex, m_queue_shutdown, m_thread_queue, and Logging::trace().

◆ loop_function_starter()

void thread_pool::loop_function_starter ( thread_pool tp)
staticprivate

Start loop function.

Parameters
[in]tp- Thread pool object of caller.

Definition at line 49 of file thread_pool.cc.

References tp.

Referenced by init().

◆ pool_size()

unsigned int thread_pool::pool_size ( ) const

Get current pool size.

Returns
Return current pool size.

Definition at line 116 of file thread_pool.cc.

References m_thread_pool.

◆ schedule_thread()

bool thread_pool::schedule_thread ( FunctionPointer &&  func)

Schedule a new thread from pool.

Parameters
[in]func- std::function object to call
Returns
Returns true if thread was successfully scheduled, false if not.

Definition at line 82 of file thread_pool.cc.

References m_queue_cond, m_queue_mutex, m_queue_shutdown, m_thread_pool, m_thread_queue, and Logging::trace().

◆ tear_down()

void thread_pool::tear_down ( bool  silent = false)

Shut down the thread pool.

Parameters
[in]silent- If true, no log messages will be issued.

Definition at line 144 of file thread_pool.cc.

References Logging::debug(), m_queue_cond, m_queue_mutex, m_queue_shutdown, m_thread_pool, and m_thread_queue.

Referenced by ~thread_pool().

Member Data Documentation

◆ m_cur_threads

unsigned int thread_pool::m_cur_threads
protected

Current number of threads.

Definition at line 122 of file thread_pool.h.

Referenced by loop_function().

◆ m_num_threads

unsigned int thread_pool::m_num_threads
protected

Max. number of threads. Defaults to 4x number of CPU cores.

Definition at line 121 of file thread_pool.h.

Referenced by init().

◆ m_queue_cond

std::condition_variable thread_pool::m_queue_cond
protected

Condition for critical section

Definition at line 118 of file thread_pool.h.

Referenced by loop_function(), schedule_thread(), and tear_down().

◆ m_queue_mutex

std::mutex thread_pool::m_queue_mutex
protected

Mutex for critical section

Definition at line 117 of file thread_pool.h.

Referenced by current_queued(), loop_function(), schedule_thread(), and tear_down().

◆ m_queue_shutdown

std::atomic_bool thread_pool::m_queue_shutdown
protected

If true all threads have been shut down

Definition at line 120 of file thread_pool.h.

Referenced by loop_function(), schedule_thread(), and tear_down().

◆ m_thread_pool

std::vector<std::thread> thread_pool::m_thread_pool
protected

Thread pool

Definition at line 116 of file thread_pool.h.

Referenced by init(), pool_size(), schedule_thread(), and tear_down().

◆ m_thread_queue

std::queue<FunctionPointer> thread_pool::m_thread_queue
protected

Thread queue parameters

Definition at line 119 of file thread_pool.h.

Referenced by current_queued(), loop_function(), schedule_thread(), and tear_down().

◆ m_threads_running

std::atomic_uint32_t thread_pool::m_threads_running
protected

Currently running threads.

Definition at line 123 of file thread_pool.h.

Referenced by current_running().


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