template<class T, class S = typename T::SharedState, class M = MutexInert>
struct evo::ThreadGroup< T, S, M >
Manages a group of threads with shared state.
- This manages a collection of threads that are started/joined together
- Each thread has access to the same state shared by all threads – the thread group owns this shared state
- This works with function-based threads and class-based threads, though there are some differences:
- Class-based threads use T derived from ThreadClass
- Thread constructor must take a SharedState reference parameter: SharedState&
- Function-based threads use T=Thread
- Caller must pass a thread function pointer to constructor (or set thread_init.func) before starting any threads
- Thread function is passed a SharedState reference parameter, cast to: SharedSatate*
- Thread safety depends on mutex type (M) – default is MutexInert (not thread safe) as this isn't usually shared between threads
- 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 to use, Thread (function-based thread) or class derived from ThreadClass (class-based thread) |
S | Shared state type to use, usually contains a Mutex or Condition for synchronization, defaults to T::SharedState |
M | Mutex type for synchronization, defaults to MutexInert (no synchronization) |
- Example with Class-Based ThreadClass
};
{ }
~MyThread()
{ thread_cancel_join(); }
void thread_run() {
}
};
int main() {
return 0;
}
- Example with Function-Based Thread
struct ThreadState {
};
static void thread_run(void* arg) {
ThreadState* state = (ThreadState*)arg;
}
int main() {
return 0;
}