You.i Engine
YiThread.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_THREAD_H_
3 #define _YI_THREAD_H_
4 
6 #include "utility/YiString.h"
7 
8 class CYIThreadPriv;
9 
15 const int32_t YI_MAX_THREADS = 100;
16 
71 class CYIThread
72 {
73  friend class CYIThreadPriv;
74 
75 public:
76  static const size_t DEFAULT_STACK_SIZE = 128 * 1024; // 128KB
77 
78  enum class Priority
79  {
80  Minimum = 0,
81  Low,
82  Normal,
83  High,
84  Maximum,
85 
86  Inherited,
87 
88  Default = Inherited
89  };
90 
91  enum class StartType
92  {
93  NonBlocking = 0,
94  BlockUntilStarted = 1,
95  };
96 
102  class Listener
103  {
104  public:
105  Listener() {}
106  virtual ~Listener() {}
107 
113  virtual void OnThreadStarted(CYIThread * /*pPublisher*/) {}
114 
120  virtual void OnThreadTerminated(CYIThread * /*pPublisher*/) {}
121 
127  virtual void OnThreadFinished(CYIThread * /*pPublisher*/) {}
128 
132  virtual void OnThreadDestroyed(CYIThread * /*pPublisher*/) {}
133  };
134 
135  CYIThread(const CYIString &name = CYIString(), Priority priority = Priority::Default, size_t stackSize = DEFAULT_STACK_SIZE);
136 
142  virtual ~CYIThread();
143 
148 
156  static const CYIThreadHandle &GetUIThreadId();
157 
161  static int32_t GetOSIdealThreadCount();
162 
168  static void Sleep(uint32_t millis);
169 
173  static void YieldCurrentThread();
174 
178  static void ReportDefaults();
179 
190 
211  void Terminate();
212 
228  void EnableTermination();
229 
241  void DisableTermination();
242 
251  bool IsTerminationEnabled();
252 
260  bool Join();
261 
271  bool Join(uint32_t timeoutMs);
272 
278  bool IsRunning() const;
279 
287 
295  void SetName(const CYIString &name);
296 
302  const CYIString &GetName() const;
303 
309  void SetPriority(Priority priority);
310 
319  Priority GetPriority() const;
320 
326  bool AddThreadListener(Listener *pListener);
327 
333  bool RemoveThreadListener(Listener *pListener);
334 
342  bool SetStackSize(size_t stackSize);
343 
349  size_t GetStackSize() const;
350 
351 protected:
352  virtual void Run() = 0;
353 
367  bool IsTerminationRequested() const;
368 
369 private:
370  // Hiding copy constructor
371  CYIThread(const CYIThread &);
372 
373  // Hiding assignment operator.
374  CYIThread &operator=(const CYIThread &);
375 
376  std::unique_ptr<CYIThreadPriv> m_pPriv;
377 };
378 
381 #endif // _YI_THREAD_H_
static const size_t DEFAULT_STACK_SIZE
Definition: YiThread.h:76
virtual void Run()=0
void SetPriority(Priority priority)
CYIThreadHandle GetThreadId() const
void Terminate()
static void Sleep(uint32_t millis)
void DisableTermination()
const int32_t YI_MAX_THREADS
Definition: YiThread.h:15
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:36
virtual ~CYIThread()
virtual void OnThreadStarted(CYIThread *)
Definition: YiThread.h:113
static void ReportDefaults()
virtual void OnThreadTerminated(CYIThread *)
Definition: YiThread.h:120
Priority GetPriority() const
const CYIString & GetName() const
bool IsTerminationEnabled()
static const CYIThreadHandle & GetUIThreadId()
bool IsRunning() const
void EnableTermination()
virtual ~Listener()
Definition: YiThread.h:106
static CYIThreadHandle GetCurrentThreadId()
static void YieldCurrentThread()
Priority
Definition: YiThread.h:78
void SetName(const CYIString &name)
virtual void OnThreadFinished(CYIThread *)
Definition: YiThread.h:127
bool SetStackSize(size_t stackSize)
bool AddThreadListener(Listener *pListener)
StartType
Definition: YiThread.h:91
Listener()
Definition: YiThread.h:105
static int32_t GetOSIdealThreadCount()
Definition: YiThread.h:102
Definition: YiThread.h:71
friend class CYIThreadPriv
Definition: YiThread.h:73
bool IsTerminationRequested() const
bool RemoveThreadListener(Listener *pListener)
virtual void OnThreadDestroyed(CYIThread *)
Definition: YiThread.h:132
size_t GetStackSize() const
CYIThread(const CYIString &name=CYIString(), Priority priority=Priority::Default, size_t stackSize=DEFAULT_STACK_SIZE)
Definition: YiThreadHandle.h:45
bool Join()
bool StartThread(StartType startType=StartType::BlockUntilStarted)