Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
sese::ThreadPool Class Reference

Thread pool class. More...

#include <ThreadPool.h>

Inheritance diagram for sese::ThreadPool:
sese::Noncopyable

Classes

struct  RuntimeData
 Runtime data of thread pool. More...
 

Public Types

using Ptr = std::unique_ptr<ThreadPool>
 

Public Member Functions

 ThreadPool (std::string thread_pool_name=THREAD_DEFAULT_NAME, size_t threads=4)
 Initialize the thread pool.
 
 ~ThreadPool () override
 
void postTask (const std::function< void()> &task)
 Add a single task to the thread pool.
 
void postTask (const std::vector< std::function< void()> > &tasks)
 Add multiple tasks to the thread pool.
 
template<typename FUNCTION , typename... ARGS>
void postTaskEx (FUNCTION &&f, ARGS &&...args)
 Add a task to the thread pool and bind parameters.
 
template<class RETURN_TYPE >
std::shared_future< RETURN_TYPE > postTask (const std::function< RETURN_TYPE()> &tasks)
 Add a task with a return value to the thread pool.
 
void shutdown ()
 Shutdown the current thread pool and block until all child threads exit.
 
size_t size () noexcept
 
bool empty () noexcept
 
const std::string & getName () const
 
size_t getThreads () const
 
- Public Member Functions inherited from sese::Noncopyable
 Noncopyable ()=default
 
virtual ~Noncopyable ()=default
 
 Noncopyable (const Noncopyable &)=delete
 
Noncopyableoperator= (const Noncopyable &)=delete
 

Private Attributes

std::string name
 
size_t threads = 0
 
std::vector< Thread * > threadGroup
 
std::shared_ptr< RuntimeDatadata
 

Detailed Description

Thread pool class.

Member Typedef Documentation

◆ Ptr

using sese::ThreadPool::Ptr = std::unique_ptr<ThreadPool>

Constructor & Destructor Documentation

◆ ThreadPool()

sese::ThreadPool::ThreadPool ( std::string thread_pool_name = THREAD_DEFAULT_NAME,
size_t threads = 4 )
explicit

Initialize the thread pool.

Parameters
thread_pool_nameName of the thread pool (affects the names of the threads in the pool)
threadsNumber of threads

References data, name, threadGroup, and threads.

◆ ~ThreadPool()

sese::ThreadPool::~ThreadPool ( )
override

References data, and shutdown().

Member Function Documentation

◆ empty()

bool sese::ThreadPool::empty ( )
nodiscardnoexcept

References data.

◆ getName()

const std::string & sese::ThreadPool::getName ( ) const
inlinenodiscard

References name.

◆ getThreads()

size_t sese::ThreadPool::getThreads ( ) const
inlinenodiscard

References threads.

◆ postTask() [1/3]

template<class RETURN_TYPE >
std::shared_future< RETURN_TYPE > sese::ThreadPool::postTask ( const std::function< RETURN_TYPE()> & tasks)

Add a task with a return value to the thread pool.

Template Parameters
RETURN_TYPEReturn type
Parameters
tasksTasks to be executed
Returns
std::shared_future object
* Since std::packaged_task is a non-copyable object,
* and std::function erases the type information of its arguments, making std::move ineffective on non-copyable objects,
* the solution here is to use std::shared_ptr to wrap std::packaged_task.
* 

References postTask().

◆ postTask() [2/3]

void sese::ThreadPool::postTask ( const std::function< void()> & task)

Add a single task to the thread pool.

Parameters
taskThe task to be executed

References data.

Referenced by sese::async(), postTask(), and postTaskEx().

◆ postTask() [3/3]

void sese::ThreadPool::postTask ( const std::vector< std::function< void()> > & tasks)

Add multiple tasks to the thread pool.

Parameters
tasksCollection of tasks to be executed

References data.

◆ postTaskEx()

template<typename FUNCTION , typename... ARGS>
void sese::ThreadPool::postTaskEx ( FUNCTION && f,
ARGS &&... args )
inline

Add a task to the thread pool and bind parameters.

Template Parameters
FUNCTIONFunction template
ARGSArgument template
Parameters
fFunction
argsArguments

References f, and postTask().

◆ shutdown()

void sese::ThreadPool::shutdown ( )

Shutdown the current thread pool and block until all child threads exit.

References data, and threadGroup.

Referenced by ~ThreadPool().

◆ size()

size_t sese::ThreadPool::size ( )
nodiscardnoexcept

References data.

Member Data Documentation

◆ data

std::shared_ptr<RuntimeData> sese::ThreadPool::data
private

◆ name

std::string sese::ThreadPool::name
private

Referenced by getName(), and ThreadPool().

◆ threadGroup

std::vector<Thread *> sese::ThreadPool::threadGroup
private

Referenced by shutdown(), and ThreadPool().

◆ threads

size_t sese::ThreadPool::threads = 0
private

Referenced by getThreads(), and ThreadPool().


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