GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
graphlab::thread_pool Class Reference

#include <graphlab/parallel/thread_pool.hpp>

List of all members.

Public Member Functions

 thread_pool (size_t nthreads=2, bool affinity=false)
void resize (size_t nthreads)
size_t size () const
void set_cpu_affinity (bool affinity)
bool get_cpu_affinity ()
void launch (const boost::function< void(void)> &spawn_function, int virtual_threadid=-1)
void join ()
 ~thread_pool ()
 Destructor. Cleans up all threads.

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 performs limited exception forwarding. exception throws within a thread of type const char* will be caught and forwarded to the join() function. If the call to join() is wrapped by a try-catch block, the exception will be caught safely and thread cleanup will be completed properly.

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 56 of file thread_pool.hpp.


Member Function Documentation

bool graphlab::thread_pool::get_cpu_affinity ( )
inline

Gets the CPU affinity.

Definition at line 123 of file thread_pool.hpp.

void graphlab::thread_pool::join ( )

Waits for all threads to become free. const char* exceptions thrown by threads are forwarded to the join() function. Once this function returns normally, the queue is empty.

Note that this function may not return if producers continually insert tasks through launch.

Definition at line 182 of file thread_pool.cpp.

void graphlab::thread_pool::launch ( const boost::function< void(void)> &  spawn_function,
int  virtual_threadid = -1 
)

Launch a single thread which calls spawn_function. If affinity is set on construction of the thread_pool, the thread handling the function will be locked on to one particular CPU.

If virtual_threadid is set, the target thread will appear to have thread ID equal to the requested thread ID

Definition at line 174 of file thread_pool.cpp.

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

Set the number of threads in the queue

Definition at line 41 of file thread_pool.cpp.

void graphlab::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.

Definition at line 111 of file thread_pool.cpp.

size_t graphlab::thread_pool::size ( ) const

Get the number of threads

Definition at line 69 of file thread_pool.cpp.


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