Lunchbox  1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::ThreadPool Class Reference

Thread pool for tasks execution. More...

#include <threadPool.h>

+ Collaboration diagram for lunchbox::ThreadPool:

Public Member Functions

 ThreadPool (const size_t size=std::max(1u, std::thread::hardware_concurrency()))
 Construct a ThreadPool. More...
 
 ~ThreadPool ()
 Destroy this thread pool. More...
 
size_t getSize () const
 
template<typename F >
std::future< typename std::result_of< F()>::type > post (F &&f)
 Post a new task in the thread pool. More...
 
template<typename F >
void postDetached (F &&f)
 Post a detached task in the thread pool. More...
 
bool hasPendingJobs () const
 

Detailed Description

Thread pool for tasks execution.

A task is a callable object taking no arguments and returing a value or void. All the member methods are thread safe.

Example:

/* Copyright (c) 2016-2017, Mohamed-Ghaith Kaabi <mohamedghaith.kaabi@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define BOOST_TEST_MODULE ThreadPool
#include <boost/test/unit_test.hpp>
#include <lunchbox/threadPool.h>
BOOST_AUTO_TEST_CASE(size)
{
lunchbox::ThreadPool threadPool{3};
BOOST_CHECK(threadPool.getSize() == 3);
}
BOOST_AUTO_TEST_CASE(queue)
{
lunchbox::ThreadPool threadPool{1};
for (int i = 0; i < 10; ++i)
{
threadPool.postDetached([] {
std::this_thread::sleep_for(
std::chrono::milliseconds(50 + rand() % 50));
});
}
// append a dummy task
threadPool.post([]() {}).get();
BOOST_CHECK(!threadPool.hasPendingJobs());
}
BOOST_AUTO_TEST_CASE(dispatcher)
{
std::vector<std::future<int> > futures;
lunchbox::ThreadPool threadPool{4};
for (int i = 0; i < 10; ++i)
{
futures.push_back(threadPool.post([] {
std::this_thread::sleep_for(
std::chrono::milliseconds(50 + rand() % 50));
return 42;
}));
}
BOOST_CHECK(threadPool.hasPendingJobs());
for (auto& future : futures)
{
BOOST_CHECK(future.get() == 42);
}
}
int task()
{
std::this_thread::sleep_for(std::chrono::milliseconds(50 + rand() % 50));
return 42;
}
BOOST_AUTO_TEST_CASE(join)
{
std::vector<std::future<int> > futures;
{
lunchbox::ThreadPool threadPool{4};
for (int i = 0; i < 100; ++i)
{
futures.push_back(threadPool.post(task));
}
} // blocks until all tasks are done
for (const std::future<int>& future : futures)
{
BOOST_CHECK(future.wait_for(std::chrono::milliseconds(0)) ==
std::future_status::ready);
}
}

Definition at line 37 of file threadPool.h.

Constructor & Destructor Documentation

lunchbox::ThreadPool::ThreadPool ( const size_t  size = std::max(1u, std::thread::hardware_concurrency()))
inline

Construct a ThreadPool.

Parameters
sizenumber of threads in the thread pool

Definition at line 19 of file threadPool.ipp.

lunchbox::ThreadPool::~ThreadPool ( )
inline

Destroy this thread pool.

Will block until all the tasks are done.

Definition at line 26 of file threadPool.ipp.

Member Function Documentation

size_t lunchbox::ThreadPool::getSize ( ) const
inline
Returns
the number of threads used in the thread pool

Definition at line 36 of file threadPool.ipp.

bool lunchbox::ThreadPool::hasPendingJobs ( ) const
inline
Returns
true if there are pending tasks to be executed.

Definition at line 68 of file threadPool.ipp.

template<typename F >
std::future< typename std::result_of< F()>::type > lunchbox::ThreadPool::post ( F &&  f)
inline

Post a new task in the thread pool.

Returns
a std::future containing the future result.

Definition at line 42 of file threadPool.ipp.

template<typename F >
void lunchbox::ThreadPool::postDetached ( F &&  f)
inline

Post a detached task in the thread pool.

The result of this task is not monitored.

Definition at line 59 of file threadPool.ipp.


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