PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
DetachPool.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include "DetachPool.h"
8
10
13std::string phoenix_statusToString(int exitStatus){
14 if(exitStatus == PEXIT_STATUS_NOT_FIHISHED){return "\033[33mStill Running\033[0m";}
15 else if(exitStatus == PEXIT_STATUS_OK){return "\033[32mSUCCESS\033[0m";}
16 else{
17 return "\033[31mFAIL\033[0m";
18 }
19}
20
22
25void phoenix_printThreadStatus(const ListThreadDetachInfo & listThreadInfo, std::ostream & out){
26 out << "ListThreadDetachInfo : number of remaning threads : " << listThreadInfo.size() << std::endl;
27 size_t i(0lu);
28 for(ListThreadDetachInfo::const_iterator it(listThreadInfo.begin()); it != listThreadInfo.end(); ++it){
29 out << "\tthread("<<i<<") '"<<it->description<<"' has status " << phoenix_statusToString(it->exitStatus) << std::endl;
30 ++i;
31 }
32}
33
38
40
43 copyDetachPool(other);
44}
45
50
52
56 copyDetachPool(other);
57 return *this;
58}
59
61
64 p_refreshTime = refreshTime;
65}
66
68
73
75
78 p_destructorWaitTime = destructorWaitTime;
79}
80
82
87
89
97
99
101void DetachPool::removeFinishedThread(std::ostream & out){
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}
117
121 if(ellapsedTimeRefresh < p_refreshTime){
122 return; //Not time yet
123 }
126}
127
129
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}
141
143
150
153 p_refreshTime = 1000000lu; //Refresh every second by default
156}
157
158
159
std::string phoenix_statusToString(int exitStatus)
Convert the current status of a thread into a string.
void phoenix_printThreadStatus(const ListThreadDetachInfo &listThreadInfo, std::ostream &out)
Print the status of all remaning thread.
std::string phoenix_statusToString(int exitStatus)
Convert the current status of a thread into a string.
std::list< ThreadDetachInfo > ListThreadDetachInfo
Definition DetachPool.h:31
#define PEXIT_STATUS_NOT_FIHISHED
Definition DetachPool.h:19
#define PEXIT_STATUS_OK
Definition DetachPool.h:20
PhoenixTime p_timeLastRefresh
Time of last refresh (in micro second)
Definition DetachPool.h:67
void removeFinishedThread(std::ostream &out=std::cout)
Remove all finished threads.
void initialisationDetachPool()
Initialisation function of the class DetachPool.
DetachPool & operator=(const DetachPool &other)
Definition of equal operator of DetachPool.
DetachPool()
Default constructor of DetachPool.
ListThreadDetachInfo getListThreadInfo()
Get the list of thread information.
virtual ~DetachPool()
Destructor of DetachPool.
PEllapsedTime p_refreshTime
Ellapsed time between two refresh (in micro second)
Definition DetachPool.h:65
PEllapsedTime p_destructorWaitTime
Wait time when object is destroyed.
Definition DetachPool.h:71
PEllapsedTime getDestructorWaitTime() const
Get the wait time on object destruction (in microseconds)
std::mutex p_mtx
Mutex to handle exit status of detached threads.
Definition DetachPool.h:63
void setRefreshTime(PEllapsedTime refreshTime)
Set the ellasped time between two refresh in microseconds.
PEllapsedTime getRefreshTime() const
Get the ellasped time between two refresh in microseconds.
void setDestructionWaitTime(PEllapsedTime destructorWaitTime)
Set the wait time on object destruction (in microseconds)
void waitUntilAllFinish(PEllapsedTime maxWaitTime=10000000)
wait until all detached thread have fnished theirs computation
ListThreadDetachInfo p_listThreadInfo
List of the information related to all ongoing threads.
Definition DetachPool.h:69
void copyDetachPool(const DetachPool &other)
Copy function of DetachPool.
void refresh()
Refresh the list of the detached threads.
PhoenixTime phoenix_thread_clock()
Get the current time.
std::chrono::time_point< std::chrono::steady_clock > PhoenixTime
PEllapsedTime phoenix_ellapsedTime(PhoenixTime referenceTime)
Compute an ellapsed time in the given unit.
int64_t PEllapsedTime