cppthread 1.1.16
C++ Thread Library
Public Member Functions | Private Attributes | List of all members
cppthread::life Class Reference

An RAII class managing the lifetime of a thread. More...

Collaboration diagram for cppthread::life:
Collaboration graph
[legend]

Public Member Functions

 life (life const &rhs)=delete
 The copy operator is deleted.
 
 life (thread *const thread)
 Initialize a "thread life" object.
 
 ~life ()
 Make sure the thread stops.
 
lifeoperator= (life const &rhs)=delete
 The assignment operator is deleted.
 

Private Attributes

threadf_thread = nullptr
 The pointer to the thread being managed.
 

Detailed Description

This class is used to manage the life of a thread: the time it runs. The constructor calls the thread::start() function and the destructor makes sure to call the thread::stop() function.

If you have a specific block or another class that should run a thread for the lifetime of the block or class object, then this is well adapted.

Note
This class is not responsible for deleting the thread at the end. It only manages the time while the thread runs.

Definition at line 40 of file life.h.

Constructor & Destructor Documentation

◆ life() [1/2]

cppthread::life::life ( thread *const  thread)

This type of objects are used to record a thread and make sure that it gets destroyed once done with it.

The constructor makes sure that the specified thread is not a null pointer and it starts the thread. If the thread is already running, then the constructor will throw.

Once such an object was created, it is not possible to prevent the thread life destructor from calling the stop() function and waiting for the thread to be done.

Note
If possible, you should consider using the thread::pointer_t instead of the life which expects a bare pointer. There are situations, though, where this class is practical because you have a thread as a variable member in a class.
Parameters
[in]threadThe thread which life is to be controlled.

Definition at line 87 of file life.cpp.

References f_thread, and cppthread::thread::start().

Here is the call graph for this function:

◆ life() [2/2]

cppthread::life::life ( life const &  rhs)
delete

The life object holds a bare pointer to the thread it has to manage, so we have to declare a copy operator to explicitly delete the copy operator. We could not have multiple instances of the life object anyway.

Parameters
[in]rhsThe right hand side.

◆ ~life()

cppthread::life::~life ( )

This function requests that the attach thread stop. It will block until such happens. You are responsible to make sure that the stop happens early on if your own object needs to access the thread while stopping.

Definition at line 111 of file life.cpp.

References f_thread, and cppthread::thread::stop().

Here is the call graph for this function:

Member Function Documentation

◆ operator=()

cppthread::life::operator= ( life const &  rhs)
delete

The life object holds a bare pointer to the thread it has to manage so we have to declare an assignment operator to explicitly delete the operator. We could not have multiple instances of the life object anyway.

Parameters
[in]rhsThe right hand side.
Returns
A reference to this object.

Member Data Documentation

◆ f_thread

cppthread::life::f_thread = nullptr
private

This pointer is a pointer to the thread. Once a thread life object is initialized, the pointer is never nullptr (we throw before in the constructor if that is the case.)

The user of the life class must view the thread pointer as owned by the life object (similar to a smart pointer.)

Definition at line 50 of file life.h.

Referenced by life(), and ~life().


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

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.