libgenua
Basic Geometry, Numerical Algorithms and Interfaces
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
WorkStack< TaskType > Class Template Reference

Detailed Description

template<class TaskType>
class WorkStack< TaskType >

Last-in, first-out task stack.

This is a synchronized stack (using std::vector) with an attached ThreadGroup. Use this task container if the algorithm benefits from temporal locality issues, that is, when you want tasks to be executed in a LIFO manner. Many tree-based or recursive algorithms benefit from this type of locality because the topmost (last) tasks typically work on data which is close to the memory block processed by the last task.

Todo:
Implement corresponing FIFO queue.
See Also
ThreadGroup

#include <taskgroup.h>

Inheritance diagram for WorkStack< TaskType >:
[legend]
Collaboration diagram for WorkStack< TaskType >:
[legend]

Public Member Functions

 WorkStack (size_t)
 create blocking stack and reserve space for tasks
 
bool submit (const TaskType &t)
 submit a task and wake up one thread
 
bool submit (const TaskType &a, const TaskType &b)
 common pattern: submit two jobs (tree recursion)
 
bool submit (TaskType &&a, TaskType &&b)
 common pattern: submit two jobs (tree recursion)
 
bool submit (size_t n, const TaskType t[])
 submit many tasks at once, only lock one time
 
void wakeup ()
 wakeup all waiting threads
 
void join ()
 joining threads requires wakeup
 
- Public Member Functions inherited from ThreadGroup
 ThreadGroup (int startThreads=0)
 create thread container, optionally spawn threads
 
virtual ~ThreadGroup ()
 join all threads, then destroy container
 
virtual void spawn (int n=-1)
 start n (default: hw threads) new threads which call overloaded method work()
 
void forkJoin (int n=-1)
 spawn threads and wait until all have completed their work
 
void requestInterruption (bool flag)
 set the termination flag
 
size_t nworker () const
 number of threads created
 

Protected Member Functions

uint pop ()
 index-based task retrieval, only works without reallocation!
 
void work (int)
 thread work function
 
bool notQuiteDone () const
 wait condition: task container drained, but someone still working
 
bool empty () const
 no tasks left
 

Private Attributes

std::deque< TaskType > m_tasks
 container for tasks objects
 
boost::mutex m_mutex
 mutex for stack access
 
boost::condition_variable m_workpending
 condition which indicates that new work is available
 
std::atomic< int > m_pending
 number of pending (submitted but not completed) tasks
 
std::atomic< int > m_popped
 number of tasks popped off until now
 

Additional Inherited Members

- Protected Attributes inherited from ThreadGroup
std::atomic< bool > m_earlyexit
 early exit flag; if this is set, then worker threads should exit
 

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