Turi Create
4.0
|
#include <core/parallel/thread_pool.hpp>
Public Member Functions | |
thread_pool (size_t nthreads=2, bool affinity=false) | |
void | resize (size_t nthreads) |
size_t | size () const |
void | launch (const boost::function< void(void)> &spawn_function, size_t virtual_threadid=size_t(-1)) |
void | set_cpu_affinity (bool affinity) |
bool | get_cpu_affinity () |
~thread_pool () | |
Destructor. Cleans up all threads. | |
Static Public Member Functions | |
static thread_pool & | get_instance () |
static void | release_instance () |
Manages a pool of threads.
The interface is nearly identical to the thread_group. The key difference is internal behavior. The thread pool preallocates a collection of threads which it keeps asleep. When tasks are issued through the "launch" function, threads are woken up to perform the tasks.
The thread_pool object does not perform exception forwarding, use parallel_task_queue for that.
If multiple threads are running in the thread-group, the master should test if running_threads() is > 0, and retry the join().
Definition at line 101 of file thread_pool.hpp.
turi::thread_pool::thread_pool | ( | size_t | nthreads = 2 , |
bool | affinity = false |
||
) |
Initializes a thread pool with nthreads. If affinity is set, the nthreads will by default stripe across the available cores on the system.
|
inline |
Gets the CPU affinity.
Definition at line 176 of file thread_pool.hpp.
|
static |
Returns a singleton instance of the thread pool
void turi::thread_pool::launch | ( | const boost::function< void(void)> & | spawn_function, |
size_t | virtual_threadid = size_t(-1) |
||
) |
Queues a single task into the thread pool which calls spawn_function.
If virtual_threadid is set, the target thread will appear to have thread ID equal to the requested thread ID.
|
static |
Frees the singleton instance of the thread pool
void turi::thread_pool::resize | ( | size_t | nthreads | ) |
Set the number of threads in the queue
void turi::thread_pool::set_cpu_affinity | ( | bool | affinity | ) |
Changes the CPU affinity. Note that pthread does not provide a way to change CPU affinity on a currently started thread. This function therefore waits for all threads in the pool to finish their current task, and destroy all the threads. Then new threads are created with the new affinity setting.
size_t turi::thread_pool::size | ( | ) | const |
Get the number of threads