FFmpegfs Fuse Multi Media Filesystem 2.16
|
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< FunctionPointer > | m_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... | |
The thread_pool class.
Definition at line 54 of file thread_pool.h.
typedef std::function<int(void)> thread_pool::FunctionPointer |
Pointer to thread pool function.
Definition at line 57 of file thread_pool.h.
|
explicit |
Construct a thread_pool object.
[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.
|
virtual |
Object destructor. Ends all threads and cleans up resources.
Definition at line 44 of file thread_pool.cc.
References tear_down().
unsigned int thread_pool::current_queued | ( | ) |
Get number of currently queued threads.
Definition at line 109 of file thread_pool.cc.
References m_queue_mutex, and m_thread_queue.
unsigned int thread_pool::current_running | ( | ) | const |
Get number of currently running threads.
Definition at line 104 of file thread_pool.cc.
References m_threads_running.
int thread_pool::init | ( | unsigned int | num_threads = 0 | ) |
Initialise thread pool. Initialise the thread pool. Does nothing if called more than once.
[in] | num_threads | - Optional: number of threads to create in pool. Defaults to Defaults to 4x number of CPU cores. |
Definition at line 121 of file thread_pool.cc.
References Logging::info(), loop_function_starter(), m_num_threads, m_thread_pool, and Logging::warning().
|
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().
|
staticprivate |
Start loop function.
[in] | tp | - Thread pool object of caller. |
Definition at line 49 of file thread_pool.cc.
References tp.
Referenced by init().
unsigned int thread_pool::pool_size | ( | ) | const |
Get current pool size.
Definition at line 116 of file thread_pool.cc.
References m_thread_pool.
bool thread_pool::schedule_thread | ( | FunctionPointer && | func | ) |
Schedule a new thread from pool.
[in] | func | - std::function object to call |
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().
void thread_pool::tear_down | ( | bool | silent = false | ) |
Shut down the thread pool.
[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().
|
protected |
Current number of threads.
Definition at line 122 of file thread_pool.h.
Referenced by loop_function().
|
protected |
Max. number of threads. Defaults to 4x number of CPU cores.
Definition at line 121 of file thread_pool.h.
Referenced by init().
|
protected |
Condition for critical section
Definition at line 118 of file thread_pool.h.
Referenced by loop_function(), schedule_thread(), and tear_down().
|
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().
|
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().
|
protected |
Thread pool
Definition at line 116 of file thread_pool.h.
Referenced by init(), pool_size(), schedule_thread(), and tear_down().
|
protected |
Thread queue parameters
Definition at line 119 of file thread_pool.h.
Referenced by current_queued(), loop_function(), schedule_thread(), and tear_down().
|
protected |
Currently running threads.
Definition at line 123 of file thread_pool.h.
Referenced by current_running().