Lunchbox  1.17.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)
 Construct a new 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
 

Static Public Member Functions

static ThreadPoolgetInstance ()
 

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 (size_t 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;
for (size_t i = 0; i < lunchbox::ThreadPool::getInstance().getSize() * 2;
++i)
{
futures.push_back(lunchbox::ThreadPool::getInstance().post([] {
std::this_thread::sleep_for(
std::chrono::milliseconds(50 + rand() % 50));
return 42;
}));
}
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 (size_t 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)

Construct a new ThreadPool.

Parameters
sizenumber of threads in the thread pool
See also
getInstance() for the recommended thread pool.
lunchbox::ThreadPool::~ThreadPool ( )

Destroy this thread pool.

Will block until all the tasks are done.

Member Function Documentation

static ThreadPool& lunchbox::ThreadPool::getInstance ( )
static
Returns
the application-global thread pool.
size_t lunchbox::ThreadPool::getSize ( ) const
Returns
the number of threads used in the thread pool
bool lunchbox::ThreadPool::hasPendingJobs ( ) const
Returns
true if there are pending tasks to be executed.
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 95 of file threadPool.h.

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 112 of file threadPool.h.


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