Turi Create  4.0
turi::thread_pool Class Reference

#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_poolget_instance ()
 
static void release_instance ()
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ thread_pool()

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.

Member Function Documentation

◆ get_cpu_affinity()

bool turi::thread_pool::get_cpu_affinity ( )
inline

Gets the CPU affinity.

Definition at line 176 of file thread_pool.hpp.

◆ get_instance()

static thread_pool& turi::thread_pool::get_instance ( )
static

Returns a singleton instance of the thread pool

◆ launch()

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.

◆ release_instance()

static void turi::thread_pool::release_instance ( )
static

Frees the singleton instance of the thread pool

◆ resize()

void turi::thread_pool::resize ( size_t  nthreads)

Set the number of threads in the queue

◆ set_cpu_affinity()

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()

size_t turi::thread_pool::size ( ) const

Get the number of threads


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