KSquare Utilities
KKThreadManager.h
Go to the documentation of this file.
1 /* KKThreadManager.cpp -- Manages the threads that perform the image extraction process.
2  * Copyright (C) 2012-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #if !defined(_KKTHREADMANAGER_)
6 #define _KKTHREADMANAGER_
7 
8 #include <fstream>
9 #include <iostream>
10 
11 #include "MsgQueue.h"
12 #include "RunLog.h"
13 using namespace KKB;
14 
15 
16 
17 namespace KKB
18 {
19 #if !defined(_KKTHREAD_)
20  class KKTHread;
21  typedef KKTHread* KKTHreadPtr;
22 #endif
23 
24 
25  /**
26  *@details
27  *
28  *@code
29  * There are three ways to stop Thread processing with the first being the most graceful and the last being the most abrupt.
30  * 1) 'ShutdownProcessing' The preferred method; each thread is flagged for 'Shutdown' after all its prerequisite shutdown threads
31  * have shutdown.
32  * 2) 'TerminateProcessing' Similar to 'ShutdownProcessing' except the prerequisites are not checked; each thread is immediately flagged
33  * to Terminate which indicates to the thread that it is to cease all processing and exit while releasing all resources.
34  * 3) 'KillAllRunningThreads' A last resort; you would call this if for some reason threads are not terminating on there own.
35  *@endcode
36  */
37  class KKThreadManager: public KKThread
38  {
39  public:
40  KKThreadManager (const KKStr& _managerName,
41  kkuint32 _maxNumThreads,
42  MsgQueuePtr _msgQueue
43  );
44 
45  ~KKThreadManager ();
46 
47  const bool& ShutdownFlag () const {return shutdownFlag;}
48  const bool& Crashed () const {return crashed;}
49  const bool& DoneExecuting () const {return doneExecuting;}
50 
51  bool AllThreadsTerminated ();
52 
53  void AddThread (KKThreadPtr _thread); /**< Add thread that is to be managed; will take ownership */
54 
55  bool AnyProcessorsCrashed ();
56 
57  void KillAllRunningThreads (); /**< Will STOP ALL running threads; NOT Grace-full; */
58 
59  void ManageThreads (bool& successful);
60 
61  void RequestShutdown ();
62 
63  void RequestTerminate ();
64 
65  virtual void Run ();
66 
67  /** @brief Shutdown all threads in a orderly way; observing prerequisite ordering. */
68  void ShutdownProcessing (kkint32 miliSecsToWait);
69 
70  void TerminateProcessing (); /**< Flag all All running threads to Terminate ASAP and release all allocated resources. */
71 
72  private:
73  void StartThreads (bool& successful);
74 
75  bool crashed; /**< Indicates if any one of the threads crashed. */
76  bool doneExecuting; /**< The last thing this instance will do in 'ManageTheExtraction'is set this flag to true. */
77  kkuint32 maxNumThreads;
78  bool shutdownFlag;
79  bool shutdownRequested;
80  bool terminateFlag;
81  bool terminateRequested;
82 
83  KKThreadListPtr threads; /**< List of all threads created under this instance of 'KKThreadManager'. It
84  * will own them.
85  */
86 
87  }; /* KKThreadManager */
88 
90 
91 
92 } /* KKB */
93 
94 
95 #endif
__int32 kkint32
Definition: KKBaseTypes.h:88
KKThread * KKThreadPtr
Definition: KKThread.h:30
const bool & DoneExecuting() const
void ShutdownProcessing(kkint32 miliSecsToWait)
Shutdown all threads in a orderly way; observing prerequisite ordering.
const bool & ShutdownFlag() const
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKThreadManager(const KKStr &_managerName, kkuint32 _maxNumThreads, MsgQueuePtr _msgQueue)
KKTHread * KKTHreadPtr
KKThreadList * KKThreadListPtr
Definition: KKThread.h:19
KKThreadManager * KKThreadManagerPtr
Definition: KKThread.h:15
The base class to be used any thread.
Definition: KKThread.h:27
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
const bool & Crashed() const
void AddThread(KKThreadPtr _thread)
void ManageThreads(bool &successful)