commonlib
threadex.h
Go to the documentation of this file.
1 
22 #pragma once
23 
24 #include "threading.h"
25 #include <thread>
26 #include <atomic>
27 #include <mutex>
28 #include "threadexceptionadapter.h"
29 
30 BEGIN_NAMESPACE_THREADING
31 
32 enum class ThreadExPriority : int
33 {
34  idle = 0,
35  lowest = 1,
36  low = 2,
37  normal = 3,
38  high = 4,
39  highest = 5,
40  timeCritical = 6
41 };
42 
46 class ThreadEx
47 {
48 public:
49  ThreadEx();
50  virtual ~ThreadEx();
51 
55  virtual void start();
56 
60  virtual void stop();
61 
65  void terminate();
66 
70  void waitFor();
71 
76  const ThreadExPriority& getPriority() const;
77 
82  void setPriority(ThreadExPriority priority);
83 
88  std::recursive_mutex& getLock();
89 
94  const std::atomic<bool>& getTerminated() const;
95 
100  const std::atomic<bool>& getFinished() const;
101 
106  bool getStarted() const;
107 
113 
118  void setExceptionAdapter(ThreadExceptionAdapter* exceptionAdapter);
119 
123  const std::thread::id getThreadId() const;
124 
125 protected:
126  std::recursive_mutex lock;
127  std::atomic<bool> terminated;
128  std::atomic<bool> finished;
129  ThreadExceptionAdapter* exceptionAdapter{};
130 
134  virtual void execute() = 0;
135 
139  void setThreadName(const char* name);
140 
141 private:
142  std::unique_ptr<std::thread> currentThread;
143  ThreadExPriority priority;
144  void mainThread();
145 };
146 
147 END_NAMESPACE_THREADING
void waitFor()
Waits for the thread to terminate.
ThreadEx class for working with threads.
Definition: threadex.h:46
const std::thread::id getThreadId() const
Returns thread id.
bool getStarted() const
Returns true if thread is started.
void setExceptionAdapter(ThreadExceptionAdapter *exceptionAdapter)
Sets exception information holder.
void terminate()
Terminate sets the thread&#39;s Terminated property to true, signaling that the thread should be terminat...
Wraps exception_ptr to deliver exception propagation from thread to the main thread.
Definition: threadexceptionadapter.h:23
An adapter managing exception_ptr useful for exception handling in threads.
virtual void stop()
Terminate and wait for the thread to terminate.
void setThreadName(const char *name)
Set current thread name (you must call it from the thread)
virtual void start()
Starts the execution of a thread.
const ThreadExPriority & getPriority() const
Get thread&#39;s scheduling priority relative to other threads in the process.
std::recursive_mutex & getLock()
Get thread&#39;s locking object.
ThreadExceptionAdapter * getExceptionAdapter()
Returns exception information holder.
const std::atomic< bool > & getTerminated() const
Returns thread&#39;s terminated signal.
void setPriority(ThreadExPriority priority)
Set thread&#39;s scheduling priority relative to other threads in the process.
virtual void execute()=0
Provides an abstract or pure virtual method to contain the code which executes when the thread is run...
const std::atomic< bool > & getFinished() const
Returns true if thread has already been finished.