Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cJobThreadSet.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cJobThreadSet_H
8 #define _INC_cJobThreadSet_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cJobBase.h"
14 #include "cThread.h"
15 
16 namespace GrayLib
17 {
18  template < class T >
20  : public cSingleton< cJobThreadPool<T> >
21  {
24 
25  typedef cJobThreadPool<T> POOL_t;
27 
28  friend class cSingleton< cJobThreadPool<T> >;
30 
31  private:
34 
35  protected:
37  : cSingleton<POOL_t>(this, typeid(POOL_t))
38  {
39  }
41  {
42  }
43 
44  public:
45  inline ITERATE_t get_FreeThreadQty() const noexcept
46  {
47  return m_aFree.GetSize();
48  }
50  {
51  // Get a free thread if i have any.
52  return m_aFree.PopTail();
53  }
55  {
56  ASSERT(pThread != nullptr);
57  ASSERT(pThread->get_Job() == nullptr);
58  ASSERT(!m_aFree.HasArg(pThread));
59  return m_aFree.Add(pThread); // to tail.
60  }
62  {
64  cThreadGuard threadguard(m_aFree.m_Lock);
65  // like RequestStopThreadArray
66  for (int i = 0; i < m_aFree.GetSize(); i++)
67  {
68  m_aFree.GetAtCheck(i)->RequestStopThread(true);
69  }
70  m_aFree.RemoveAll();
71  }
72  };
73 
74  template < class T >
76  {
80 
81  protected:
82  const int m_iMinThreads;
83  const int m_iMaxThreads;
84 
86 
88 
89  public:
90  inline cJobThreadSet(int iMinThreads = 0, int iMaxThreads = 32) noexcept
91  : m_iMinThreads(iMinThreads)
92  , m_iMaxThreads(iMaxThreads)
93  {
94  }
95 
96  inline ITERATE_t get_RunningJobQty() const noexcept
97  {
98  return m_aRunningThreads.GetSize();
99  }
100  inline ITERATE_t get_WaitingJobQty() const noexcept
101  {
102  return m_WaitingJobs.GetSize();
103  }
104  };
105 }
106 #endif
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
Definition: cJobBase.h:175
Definition: cJobThreadSet.h:21
~cJobThreadPool()
Definition: cJobThreadSet.h:40
ITERATE_t get_FreeThreadQty() const noexcept
Definition: cJobThreadSet.h:45
cJobThreadPool()
Definition: cJobThreadSet.h:36
void ClearFreeThreads()
Definition: cJobThreadSet.h:61
ITERATE_t AddFreeThread(T *pThread)
Definition: cJobThreadSet.h:54
cRefPtr< T > PopFreeThread()
Definition: cJobThreadSet.h:49
Definition: cJobThreadSet.h:76
cJobArray m_WaitingJobs
cJobBase to schedule some TIME later. (not yet occupying a Thread.)
Definition: cJobThreadSet.h:87
cArrayRef< T > m_aRunningThreads
Active Threads currently running/sleeping on Jobs/scripts. Idle threads get returned back to the shar...
Definition: cJobThreadSet.h:85
cJobThreadSet(int iMinThreads=0, int iMaxThreads=32) noexcept
Definition: cJobThreadSet.h:90
const int m_iMinThreads
min Threads i should keep in a pool ready to go. TODO
Definition: cJobThreadSet.h:82
ITERATE_t get_WaitingJobQty() const noexcept
Definition: cJobThreadSet.h:100
ITERATE_t get_RunningJobQty() const noexcept
Definition: cJobThreadSet.h:96
const int m_iMaxThreads
max total Threads I'm allowed to use for this set.
Definition: cJobThreadSet.h:83
ITERATE_t GetSize() const noexcept
Definition: cArray.h:137
Definition: cLocker.h:72
Definition: cRefPtr.h:225
Definition: cSingleton.h:127
bool HasArg(TYPE *pObj) const
Definition: cThreadArray.h:138
cRefPtr< TYPE > GetAtCheck(ITERATE_t nIndex) const
Definition: cThreadArray.h:117
void RemoveAll()
Definition: cThreadArray.h:162
cRefPtr< TYPE > PopTail()
Definition: cThreadArray.h:132
ITERATE_t GetSize() const
Definition: cThreadArray.h:110
cThreadLockCount m_Lock
Definition: cThreadArray.h:101
ITERATE_t Add(TYPE *pObj)
Definition: cThreadArray.h:144
ITERATE_t GetSize() const
Definition: cThreadArray.h:346
Definition: cMesh.h:22
int ITERATE_t
like size_t but signed
Definition: Index.h:28