Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::threads::ThreadGroup Class Referencefinal

Thread group class. More...

#include <ThreadGroup.h>

Public Member Functions

 ThreadGroup ()=default
 Default constructor.
 
 ~ThreadGroup ()
 Destructor.
 
 ThreadGroup (const ThreadGroup &)=delete
 Copy constructor deleted.
 
ThreadGroupoperator= (const ThreadGroup &)=delete
 Copy assignment operator deleted.
 
 ThreadGroup (ThreadGroup &&)=delete
 Move constructor deleted.
 
ThreadGroupoperator= (ThreadGroup &&)=delete
 Move assignment operator deleted.
 
bool IsThisThreadIn () const
 Is current thread in group. More...
 
bool IsThreadIn (std::thread *threadPtr) const
 Is given thread in group. More...
 
bool IsThreadIn (const std::thread::id &id) const
 Is given thread in group. More...
 
template<typename F >
std::thread * CreateThread (F &&threadfunction)
 Create and add thread to group. More...
 
void AddThread (std::thread *threadPtr)
 Add thread to group. More...
 
void RemoveThread (std::thread *threadPtr)
 Remove thread from group. More...
 
std::thread * RemoveThread (const std::thread::id &id)
 Remove thread from group. More...
 
bool JoinAll ()
 Call join on all registered threads. More...
 
size_t Size () const
 Get the number of threads registered. More...
 
bool Empty () const
 Check if we have any threads registered. More...
 

Private Types

using thread_list = std::list< std::thread * >
 Typedef for thread list type.
 
using thread_list_iter = thread_list::iterator
 Typedef for thread list iterator type.
 

Private Member Functions

bool IsThisThreadInNoMutex () const
 Is current thread in group (no mutex). More...
 
bool IsThreadInNoMutex (const std::thread::id &id) const
 Is given thread in group (no mutex). More...
 

Static Private Member Functions

static void DeleteThread (std::thread *threadPtr)
 Delete thread object. More...
 

Private Attributes

std::mutex m_mutex
 Access mutex for private data.
 
thread_list m_threadGroup
 List containing threads.
 

Detailed Description

Thread group class.

This class implements a thread group similar to boost's example implementation but using std::thread instead of boost::thread.

It is the responsibility of the caller of ~ThreadGroup() to make sure that all threads held in the thread group have been joined. This can be done by calling ThreadGroup::JoinAll().

Member Function Documentation

◆ AddThread()

void core_lib::threads::ThreadGroup::AddThread ( std::thread *  threadPtr)

Add thread to group.

Parameters
[in]threadPtr- Pointer to thread.

Throws std::runtime_error if called with a thread that already belongs to the thread group.

◆ CreateThread()

template<typename F >
std::thread* core_lib::threads::ThreadGroup::CreateThread ( F &&  threadfunction)
inline

Create and add thread to group.

Parameters
[in]threadfunction- Thread function to use with created thread.
Returns
Pointer to the created thread.

◆ DeleteThread()

void core_lib::threads::ThreadGroup::DeleteThread ( std::thread *  threadPtr)
staticprivate

Delete thread object.

Parameters
[in]threadPtr- Pointer to thread.

◆ Empty()

bool core_lib::threads::ThreadGroup::Empty ( ) const

Check if we have any threads registered.

Returns
True if no threads registered, false otherwise.

◆ IsThisThreadIn()

bool core_lib::threads::ThreadGroup::IsThisThreadIn ( ) const

Is current thread in group.

Returns
True if in group, false otherwise.

◆ IsThisThreadInNoMutex()

bool core_lib::threads::ThreadGroup::IsThisThreadInNoMutex ( ) const
private

Is current thread in group (no mutex).

Returns
True if in group, false otherwise.

◆ IsThreadIn() [1/2]

bool core_lib::threads::ThreadGroup::IsThreadIn ( std::thread *  threadPtr) const

Is given thread in group.

Parameters
[in]threadPtr- Pointer to thread.
Returns
True if in group, false otherwise.

◆ IsThreadIn() [2/2]

bool core_lib::threads::ThreadGroup::IsThreadIn ( const std::thread::id &  id) const

Is given thread in group.

Parameters
[in]id- Thread ID.
Returns
True if in group, false otherwise.

◆ IsThreadInNoMutex()

bool core_lib::threads::ThreadGroup::IsThreadInNoMutex ( const std::thread::id &  id) const
private

Is given thread in group (no mutex).

Parameters
[in]id- Thread ID.
Returns
True if in group, false otherwise.

◆ JoinAll()

bool core_lib::threads::ThreadGroup::JoinAll ( )

Call join on all registered threads.

Returns
True if join successful, false otherwise.

◆ RemoveThread() [1/2]

void core_lib::threads::ThreadGroup::RemoveThread ( std::thread *  threadPtr)

Remove thread from group.

Parameters
[in]threadPtr- Pointer to thread.

◆ RemoveThread() [2/2]

std::thread * core_lib::threads::ThreadGroup::RemoveThread ( const std::thread::id &  id)

Remove thread from group.

Parameters
[in]id- Thread ID.
Returns
Pointer to thread.

This function returns nullptr if the id cannot be found. Also this function should only be called if the thread for this ID has not been joined else the ID will be invalid.

◆ Size()

size_t core_lib::threads::ThreadGroup::Size ( ) const

Get the number of threads registered.

Returns
Number of threads.

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