Library of Assembled Shared Sources
ThreadPool Class Reference

Production-Consumer concurrency pattern. More...

#include <thread_pool.h>

Detailed Description

Production-Consumer concurrency pattern.

Author
Bramz
#include <lass/util/bind.h>
using namespace lass::util;
void parallelWork(int begin, int end);
const int n = 1000000;
const int step = 1000;
util::ThreadPool<> pool;
for (int i = 0; i < n; i += step)
{
pool.addTask(bind(parallelWork, i, std::min(i + step, n)));
}
pool.completeAllTasks();
general utility, debug facilities, ...

Good-Practice

Using the policies and constructor parameters, the ThreadPool can be operated in many different configurations. However, there are two that are particular interesting:

Best used when short bursts of a bounded number of tasks are followed by completeAllTasks(). Example: A single task is splitted in a parallel subtasks, all feeded to the pool at once, and completeAllTasks() is called to finish the grand tasks.

  • IdlePolicy: Spinning, for a fast reaction time
  • ParticipationPolicy: SelfParticipating, so that during completeAllTasks() the cpu cycles of the control thread are not wasted by spinning.
  • numberOfThreads: autoNumberOfThreads
  • maxNumberOfTasksInQueue: unlimitedNumberOfTasks, because the number of tasks in the queue can be (and should be) bounded by the application itself, and we want the control thread to go in completeAllTasks() ASAP, so it can donate cpu cycles to completing the tasks instead of waisting them spinning until another task can be added to the queue.

Best used when a continues (unbounded) stream of tasks reaches a server.

  • IdlePolicy: Signaled, to avoid needing an extra cpu for the control thread.
  • ParticipationPolicy: NotParticipating, because you'll never call completeAllTasks()
  • numberOfThreads: autoNumberOfThreads
  • maxNumberOfTasksInQueue: to avoid an excessive queue size

Policies

IdlePolicy

class IdlePolicy
{
protected:
void sleepProducer();
void wakeProducer();
void sleepConsumer();
void wakeConsumer();
void wakeAllConsumers();
};

ParticipationPolicy

template <typename TaskType, typename ConsumerType, typename IdlePolicy>
class ParticipationPolicy: public IdlePolicy
{
protected:
NotParticipating(const ConsumerType& prototype);
const unsigned numDynamicThreads(unsigned numThreads) const;
template <typename Queue> bool participate(Queue&);
};
implementation of ThreadPool's ParticipationPolicy

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