#include <evo/thread.h>
template<class T = Thread>
struct evo::ThreadScope< T >
Creates a single thread of execution in given scope.
- This starts the thread in the constructor, and cancels + joins it in destructor
- This wraps and inherits an existing thread type (T)
- Depending on thread type (T), this will be a function-based thread (see ThreadScope<Thread>) or a class-based thread (this)
- Class-based thread (T):
- Derives from ThreadClass
- Supports a cancellation mechanism – though the thread may ignore the cancel flag
- Linking:
- Linux/Unix:
-pthread
- Cygwin:
-lpthread
- Windows: Usually multithreaded by default – MSVC project settings:
C/C++ -> Code Generation -> Runtime Library
- Template Parameters
-
T | Thread type: Thread for function-based thread (default), or type derived from ThreadClass for class-based thread |
- Example – Class-Based Thread
public:
void thread_run() {
}
};
int main() {
{
}
return 0;
}
◆ This
◆ ThreadScope()
Constructor starts thread.
◆ ~ThreadScope()
Destructor cancels and joins thread.
◆ active()
Get whether thread is active (running).
- Returns
- Whether active
◆ cancel()
This& cancel |
( |
bool |
locked = false | ) |
|
|
inline |
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
◆ cancel_join()
bool 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
◆ handle()
Get platform-specific thread handle.
- Returns
- Thread handle
◆ 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
◆ 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)
The documentation for this struct was generated from the following file: