PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
DetachPool Class Reference

Pool of detached threads. More...

#include <DetachPool.h>

Public Member Functions

template<typename _Callable, typename... _Args>
void addThread (const std::string &description, _Callable &&__f, _Args &&... __args)
 Add the thread into the pool.
 
 DetachPool ()
 Default constructor of DetachPool.
 
 DetachPool (const DetachPool &other)
 Copy constructor of DetachPool.
 
PEllapsedTime getDestructorWaitTime () const
 Get the wait time on object destruction (in microseconds)
 
ListThreadDetachInfo getListThreadInfo ()
 Get the list of thread information.
 
PEllapsedTime getRefreshTime () const
 Get the ellasped time between two refresh in microseconds.
 
DetachPooloperator= (const DetachPool &other)
 Definition of equal operator of DetachPool.
 
void refresh ()
 Refresh the list of the detached threads.
 
void removeFinishedThread (std::ostream &out=std::cout)
 Remove all finished threads.
 
void setDestructionWaitTime (PEllapsedTime destructorWaitTime)
 Set the wait time on object destruction (in microseconds)
 
void setRefreshTime (PEllapsedTime refreshTime)
 Set the ellasped time between two refresh in microseconds.
 
void waitUntilAllFinish (PEllapsedTime maxWaitTime=10000000)
 wait until all detached thread have fnished theirs computation
 
virtual ~DetachPool ()
 Destructor of DetachPool.
 

Protected Member Functions

void copyDetachPool (const DetachPool &other)
 Copy function of DetachPool.
 

Private Member Functions

void initialisationDetachPool ()
 Initialisation function of the class DetachPool.
 

Private Attributes

PEllapsedTime p_destructorWaitTime
 Wait time when object is destroyed.
 
ListThreadDetachInfo p_listThreadInfo
 List of the information related to all ongoing threads.
 
std::mutex p_mtx
 Mutex to handle exit status of detached threads.
 
PEllapsedTime p_refreshTime
 Ellapsed time between two refresh (in micro second)
 
PhoenixTime p_timeLastRefresh
 Time of last refresh (in micro second)
 

Detailed Description

Pool of detached threads.

Definition at line 34 of file DetachPool.h.

Constructor & Destructor Documentation

◆ DetachPool() [1/2]

DetachPool::DetachPool ( )

Default constructor of DetachPool.

Definition at line 35 of file DetachPool.cpp.

35 {
37}
void initialisationDetachPool()
Initialisation function of the class DetachPool.

References initialisationDetachPool().

Referenced by copyDetachPool(), DetachPool(), and operator=().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DetachPool() [2/2]

DetachPool::DetachPool ( const DetachPool & other)

Copy constructor of DetachPool.

Parameters
other: class to copy

Definition at line 42 of file DetachPool.cpp.

42 {
43 copyDetachPool(other);
44}
void copyDetachPool(const DetachPool &other)
Copy function of DetachPool.

References copyDetachPool(), and DetachPool().

+ Here is the call graph for this function:

◆ ~DetachPool()

DetachPool::~DetachPool ( )
virtual

Destructor of DetachPool.

Definition at line 47 of file DetachPool.cpp.

47 {
49}
PEllapsedTime p_destructorWaitTime
Wait time when object is destroyed.
Definition DetachPool.h:71
void waitUntilAllFinish(PEllapsedTime maxWaitTime=10000000)
wait until all detached thread have fnished theirs computation

References p_destructorWaitTime, and waitUntilAllFinish().

+ Here is the call graph for this function:

Member Function Documentation

◆ addThread()

template<typename _Callable, typename... _Args>
void DetachPool::addThread ( const std::string & description,
_Callable && __f,
_Args &&... __args )

Add the thread into the pool.

Parameters
description: description of the thread (will be used in the stdout to inform when the thread is finished)
__f: function to be lauched on each thread (function with prototype void f(const std::vector<std::string>& listFile, __args); )
__args: extra arguments to be passed to the function __f (optional arguments) Passed function has to have the following prototype : void f(int & exitStatus, other arguments...);

Definition at line 20 of file DetachPool_impl.h.

20 {
21 ThreadDetachInfo info;
22 info.description = description;
24 p_mtx.lock();
25 p_listThreadInfo.push_back(info); //Let's add the info to the running threads
26 p_mtx.unlock();
27 std::thread thrSimpleDetach(__f, std::ref(p_listThreadInfo.back().exitStatus), __args...);
28 thrSimpleDetach.detach();
29}
#define PEXIT_STATUS_NOT_FIHISHED
Definition DetachPool.h:19
std::mutex p_mtx
Mutex to handle exit status of detached threads.
Definition DetachPool.h:63
ListThreadDetachInfo p_listThreadInfo
List of the information related to all ongoing threads.
Definition DetachPool.h:69
int exitStatus
Exit status of the current thread.
Definition DetachPool.h:28
std::string description
Description of the current thread.
Definition DetachPool.h:26

References ThreadDetachInfo::description, ThreadDetachInfo::exitStatus, p_listThreadInfo, p_mtx, and PEXIT_STATUS_NOT_FIHISHED.

◆ copyDetachPool()

void DetachPool::copyDetachPool ( const DetachPool & other)
protected

Copy function of DetachPool.

Parameters
other: class to copy

Definition at line 145 of file DetachPool.cpp.

145 {
149}
PhoenixTime p_timeLastRefresh
Time of last refresh (in micro second)
Definition DetachPool.h:67
PEllapsedTime p_refreshTime
Ellapsed time between two refresh (in micro second)
Definition DetachPool.h:65

References DetachPool(), p_destructorWaitTime, p_refreshTime, and p_timeLastRefresh.

Referenced by DetachPool(), and operator=().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getDestructorWaitTime()

PEllapsedTime DetachPool::getDestructorWaitTime ( ) const

Get the wait time on object destruction (in microseconds)

Returns
wait time on object destruction (in microseconds)

Definition at line 84 of file DetachPool.cpp.

84 {
86}

References p_destructorWaitTime.

◆ getListThreadInfo()

ListThreadDetachInfo DetachPool::getListThreadInfo ( )

Get the list of thread information.

Returns
list of thread information

Definition at line 91 of file DetachPool.cpp.

91 {
92 p_mtx.lock();
94 p_mtx.unlock();
95 return tmp;
96}
std::list< ThreadDetachInfo > ListThreadDetachInfo
Definition DetachPool.h:31

References p_listThreadInfo, and p_mtx.

◆ getRefreshTime()

PEllapsedTime DetachPool::getRefreshTime ( ) const

Get the ellasped time between two refresh in microseconds.

Returns
ellasped time between two refresh in microseconds

Definition at line 70 of file DetachPool.cpp.

70 {
71 return p_refreshTime;
72}

References p_refreshTime.

◆ initialisationDetachPool()

void DetachPool::initialisationDetachPool ( )
private

Initialisation function of the class DetachPool.

Definition at line 152 of file DetachPool.cpp.

152 {
153 p_refreshTime = 1000000lu; //Refresh every second by default
156}
PhoenixTime phoenix_thread_clock()
Get the current time.

References p_destructorWaitTime, p_refreshTime, p_timeLastRefresh, and phoenix_thread_clock().

Referenced by DetachPool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=()

DetachPool & DetachPool::operator= ( const DetachPool & other)

Definition of equal operator of DetachPool.

Parameters
other: class to copy
Returns
copied class

Definition at line 55 of file DetachPool.cpp.

55 {
56 copyDetachPool(other);
57 return *this;
58}

References copyDetachPool(), and DetachPool().

+ Here is the call graph for this function:

◆ refresh()

void DetachPool::refresh ( )

Refresh the list of the detached threads.

Definition at line 119 of file DetachPool.cpp.

119 {
121 if(ellapsedTimeRefresh < p_refreshTime){
122 return; //Not time yet
123 }
126}
void removeFinishedThread(std::ostream &out=std::cout)
Remove all finished threads.
PEllapsedTime phoenix_ellapsedTime(PhoenixTime referenceTime)
Compute an ellapsed time in the given unit.
int64_t PEllapsedTime

References p_refreshTime, p_timeLastRefresh, phoenix_ellapsedTime(), phoenix_thread_clock(), and removeFinishedThread().

+ Here is the call graph for this function:

◆ removeFinishedThread()

void DetachPool::removeFinishedThread ( std::ostream & out = std::cout)

Remove all finished threads.

Parameters
[out]out: ostream to be used to print when a thread finishes

Definition at line 101 of file DetachPool.cpp.

101 {
102 if(p_listThreadInfo.size() == 0lu){return;}
103 p_mtx.lock();
104 std::list<ThreadDetachInfo>::iterator it(p_listThreadInfo.begin());
105 while(it != p_listThreadInfo.end()){
106 if(it->exitStatus != PEXIT_STATUS_NOT_FIHISHED){ //Thread is finished (in good or bad), so we can remove its informations
107 if(it->description != ""){
108 out << "DetachPool : thread '"<<it->description<<"' finished with status " << phoenix_statusToString(it->exitStatus) << std::endl;
109 }
110 it = p_listThreadInfo.erase(it);
111 }else{
112 ++it;
113 }
114 }
115 p_mtx.unlock();
116}
std::string phoenix_statusToString(int exitStatus)
Convert the current status of a thread into a string.

References p_listThreadInfo, p_mtx, PEXIT_STATUS_NOT_FIHISHED, and phoenix_statusToString().

Referenced by refresh(), and waitUntilAllFinish().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setDestructionWaitTime()

void DetachPool::setDestructionWaitTime ( PEllapsedTime destructorWaitTime)

Set the wait time on object destruction (in microseconds)

Parameters
destructorWaitTime: wait time on object destruction (in microseconds)

Definition at line 77 of file DetachPool.cpp.

77 {
78 p_destructorWaitTime = destructorWaitTime;
79}

References p_destructorWaitTime.

◆ setRefreshTime()

void DetachPool::setRefreshTime ( PEllapsedTime refreshTime)

Set the ellasped time between two refresh in microseconds.

Parameters
refreshTime: ellasped time between two refresh in microseconds

Definition at line 63 of file DetachPool.cpp.

63 {
64 p_refreshTime = refreshTime;
65}

References p_refreshTime.

◆ waitUntilAllFinish()

void DetachPool::waitUntilAllFinish ( PEllapsedTime maxWaitTime = 10000000)

wait until all detached thread have fnished theirs computation

Parameters
maxWaitTime: maximum waiting time until we kill every remaning threads (in micro second, wait 10 seconds by default)

Definition at line 131 of file DetachPool.cpp.

131 {
132 if(p_listThreadInfo.size() == 0lu){return;}
133
134 PhoenixTime startWaitingUnitThreadEnd = phoenix_thread_clock();
135
136 while(phoenix_ellapsedTime<PNanoSecond>(startWaitingUnitThreadEnd)/1000l < maxWaitTime && p_listThreadInfo.size() != 0lu){
138 usleep(p_refreshTime); //Let's wait a bit until next check
139 }
140}
std::chrono::time_point< std::chrono::steady_clock > PhoenixTime

References p_listThreadInfo, p_refreshTime, phoenix_ellapsedTime(), phoenix_thread_clock(), and removeFinishedThread().

Referenced by ~DetachPool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ p_destructorWaitTime

PEllapsedTime DetachPool::p_destructorWaitTime
private

Wait time when object is destroyed.

Definition at line 71 of file DetachPool.h.

Referenced by copyDetachPool(), getDestructorWaitTime(), initialisationDetachPool(), setDestructionWaitTime(), and ~DetachPool().

◆ p_listThreadInfo

ListThreadDetachInfo DetachPool::p_listThreadInfo
private

List of the information related to all ongoing threads.

Definition at line 69 of file DetachPool.h.

Referenced by addThread(), getListThreadInfo(), removeFinishedThread(), and waitUntilAllFinish().

◆ p_mtx

std::mutex DetachPool::p_mtx
private

Mutex to handle exit status of detached threads.

Definition at line 63 of file DetachPool.h.

Referenced by addThread(), getListThreadInfo(), and removeFinishedThread().

◆ p_refreshTime

PEllapsedTime DetachPool::p_refreshTime
private

Ellapsed time between two refresh (in micro second)

Definition at line 65 of file DetachPool.h.

Referenced by copyDetachPool(), getRefreshTime(), initialisationDetachPool(), refresh(), setRefreshTime(), and waitUntilAllFinish().

◆ p_timeLastRefresh

PhoenixTime DetachPool::p_timeLastRefresh
private

Time of last refresh (in micro second)

Definition at line 67 of file DetachPool.h.

Referenced by copyDetachPool(), initialisationDetachPool(), and refresh().


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