#include <evo/thread.h>
Base class for managing a single class-based thread of execution.
- A class-based thread is implemented by inheriting this and implementing thread_run()
- The thread is finished when thread_run() returns
- For long running threads the thread_run() method should call cancel_check() regularly, and return ASAP when cancelled – otherwise the parent thread can hang waiting on it
- Synchronization objects and shared state should be stored as member variables
- WARNING: Thread must be stopped before ThreadClass destructor is called – otherwise it will crash (abort or segfault)
- See Thread for function-based thread
- See also ThreadScope for creating a simple scoped thread, and ThreadGroup for creating a group of threads
- Linking:
- Linux/Unix:
-pthread
- Cygwin:
-lpthread
- Windows: Usually multithreaded by default – MSVC project settings:
C/C++ -> Code Generation -> Runtime Library
- Example
public:
~MyThread() {
}
}
};
int main() {
MyThread thread;
thread.thread_start();
thread.thread_cancel_join();
return 0;
}
|
typedef std::function< void(void *)> | Func |
| Thread function type – with C++11 supports lambda/functor, otherwise just function pointer More...
|
|
|
static ulong | id () |
| Get current thread ID from system. More...
|
|
static void | yield () |
| Yield control to another thread or process. More...
|
|
◆ Func
typedef std::function<void (void*)> Func |
|
inherited |
Thread function type – with C++11 supports lambda/functor, otherwise just function pointer
◆ ThreadClass()
◆ ~ThreadClass()
Destructor.
- Derived class destructor should call thread_cancel_join() to stop the thread
- WARNING: This crashes (calls abort()) if thread is still running – though the running thread may segfault first since at this point the thread object is already partially destroyed
◆ cancel_check()
Check if thread has been cancelled.
- Cancellation is not enforced, the thread has to call cancel_check() regularly to check if it's been cancelled, and stop when cancelled
- If the thread does not properly stop itself when cancelled, this can hang the parent thread waiting for it
- Returns
- Whether thread has been cancelled
◆ id()
Get current thread ID from system.
- Returns
- Current thread ID
◆ thread_active()
bool thread_active |
( |
| ) |
const |
|
inlineinherited |
Get whether thread is active (running).
- This gets whether the thread was started but hasn't been joined yet – the thread may have terminated though
- Returns
- Whether active, true if thread has started and hasn't been joined yet
◆ thread_cancel()
Set cancel flag to signal thread to stop, and wake thread via condition object.
- Cancellation is not enforced, the thread has to call cancel_check() regularly to check if it's been cancelled
- After setting cancel flag, this notifies on condmutex to wake thread
- This always unlocks condmutex when done
- Parameters
-
locked | Whether condmutex object is already locked |
- Returns
- This
◆ thread_cancel_join()
bool thread_cancel_join |
( |
bool |
locked = false | ) |
|
|
inline |
Cancels and joins thread.
- Parameters
-
locked | Passed to thread_cancel(): Whether condmutex object is already locked |
- Returns
- Whether successful, false on error (out of memory/resources) or if thread not started
◆ thread_handle()
Get platform-specific thread handle.
- Returns
- Thread handle
◆ thread_join()
Join thread by waiting for thread to stop.
- Class-based thread: May need to call thread_cancel() and wake the thread first, depending on thread implementation
- Returns
- Whether successful, false on error (out of memory/resources) or if thread not started
◆ thread_run()
virtual void thread_run |
( |
| ) |
|
|
pure virtual |
Thread run method.
- This is the thread implementation
- This should NOT use anything in base class (ThreadClass) beginning with "thread_", otherwise results are undefined
- Cancellation is not enforced, this has to call cancel_check() regularly to check if it's been cancelled, and if cancelled return ASAP from this method
- This should NOT block indefinitely without at least checking for cancellation – otherwise this can hang the parent thread
◆ thread_start()
Start thread.
- The thread will start immediately, possibly even before this returns
- Returns
- Whether successful, false on error (out of memory/resources or thread_init.func=NULL)
◆ yield()
Yield control to another thread or process.
◆ condmutex
Condition and mutex object for thread, used to wake up thread.
◆ thread_active_
◆ thread_impl_
◆ thread_init
The documentation for this struct was generated from the following file: