cpp-pthread  (v1.7.3)
Simple C++ wrapper to pthread functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Public Member Functions | List of all members
pthread::thread_group Class Reference

#include <thread.hpp>

Public Member Functions

 thread_group (bool destructor_joins_first=false) noexcept
 
virtual ~thread_group ()
 
void add (abstract_thread *thread)
 
void start ()
 
void join ()
 
unsigned long size ()
 
const bool destructor_joins_first ()
 

Detailed Description

Group of abstract_threads pointers.

This helper class is in charge of handling group of threads as a whole. Method in this class apply to all threads in the group.

When destroyed, a thread_group deletes the thread that were registered/added to it.


int main(int argc, const char * argv[]) {
  pthread::thread_group threads; // this instance will free any registered thread when it will get out of scope
  for (auto x = 10 ; x > 0 ; x--){
    threads.add( new worker("herbert")); // register threads, they will run when start() is called
  }
  threads.start(); // start running all threads
  threads.join(); // wait for registered threads to join
} // scope end
Author
herbert koelman (herbe.nosp@m.rt.k.nosp@m.oelma.nosp@m.n@me.nosp@m..com)
Since
1.3
Examples:
synchronized_queue_tests.cpp.

Definition at line 294 of file thread.hpp.

Constructor & Destructor Documentation

pthread::thread_group::thread_group ( bool  destructor_joins_first = false)
explicitnoexcept

Setup a thread container/list.

Parameters
destructor_joins_firstif true then destructor tries to wait for all registered threads to join the calling one before deleting thread instances.

Definition at line 158 of file thread.cpp.

pthread::thread_group::~thread_group ( )
virtual

delete all abstract_thread referenced by the thread_group.

If destructor_joins_first is true then the method abstract_thread::join() is called before deleting the referenced abstract_thread.

Definition at line 163 of file thread.cpp.

Member Function Documentation

void pthread::thread_group::add ( pthread::abstract_thread thread)
Parameters
threadadd/register a thread.
Examples:
synchronized_queue_tests.cpp.

Definition at line 187 of file thread.cpp.

const bool pthread::thread_group::destructor_joins_first ( )
inline
Returns
current value of destructor_joins_first property. If true the destructor shall try to join registered threads before destroying them.

Definition at line 337 of file thread.hpp.

void pthread::thread_group::join ( )

Wait for all registered threads to join the caller thread.

The method iterates the list of abstract_thread and calls the join method of each entry found.

See also
abstract_thread::join()
Examples:
synchronized_queue_tests.cpp.

Definition at line 198 of file thread.cpp.

unsigned long pthread::thread_group::size ( )
Returns
the number of threads in the thread_group

Definition at line 206 of file thread.cpp.

void pthread::thread_group::start ( )

Start running all registered threads.

See also
add(abstract_thread *thread)
Examples:
synchronized_queue_tests.cpp.

Definition at line 192 of file thread.cpp.


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