Skip to content
Marc Claesen edited this page Oct 11, 2013 · 3 revisions

This class offers thread pool functionality, based exclusively on C++11 features. The thread pool allows you to specify the number of threads or defaults to std::thread::hardware_concurrency() if unspecified.

The thread pool can also be used with a maximum number of jobs. When the maximum amount of jobs is reached, the thread pool will block when attempts are made to add new jobs to the queue until the queue length is small enough again.

The thread pool class has the following key interface:

template<
	typename Ret,     // return type of the function to be executed in parallel
	typename... Args  // arguments to the function to be executed in parallel
>
class ThreadPool<Ret(Args...)>{
public:
	typedef typename std::deque<std::future<Ret>>::iterator iterator;
	typedef typename std::deque<std::future<Ret>>::const_iterator const_iterator;
	typedef std::function<Ret(Args...)> Fun;

	/**
	 * Creates a ThreadPool to execute fun.
	 *
	 * The default number of threads is std::thread::hardware_concurrency().
	 * Using this constructor allows an infinite job queue.
	 */
	ThreadPool(Fun&& fun);

	/**
	 * Adds a new job to this ThreadPool's job queue.
	 */
	void addjob(Args... arguments);

	/**
	 * Iterators over the futures() associated with each job.
	 * Futures are presented in the order the jobs were added.
	 */
	iterator begin();
	iterator end();
};

The thread pool executes one specific function in parallel. Only the arguments may vary between jobs. This restriction reduces the overhead slightly and is all we currently need in EnsembleSVM, hence it's being imposed.

An example use case of the ThreadPool in EnsembleSVM is training base models.