Evo C++ Library v0.5.1
Public Types | Public Member Functions | Public Attributes | Protected Types | Protected Attributes | List of all members
ThreadGroup< T, S, M > Struct Template Reference

#include <evo/thread.h>

Detailed Description

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.

Template Parameters
TThread type to use, Thread (function-based thread) or class derived from ThreadClass (class-based thread)
SShared state type to use, usually contains a Mutex or Condition for synchronization, defaults to T::SharedState
MMutex type for synchronization, defaults to MutexInert (no synchronization)
Example with Class-Based ThreadClass
#include <evo/thread.h>
using namespace evo;
struct MyThread : public ThreadClass {
struct SharedState {
// Shared thread state
};
SharedState& state;
MyThread(SharedState& state) : state(state)
{ }
~MyThread()
{ thread_cancel_join(); }
void thread_run() {
// Thread code
}
};
int main() {
threads.start(5);
threads.join();
return 0;
}
Example with Function-Based Thread
#include <evo/thread.h>
using namespace evo;
struct ThreadState {
// Shared thread state
};
static void thread_run(void* arg) {
ThreadState* state = (ThreadState*)arg;
// Thread code
}
int main() {
ThreadGroup<Thread,ThreadState> threads(thread_run);
threads.start(5);
threads.join();
return 0;
}

Public Types

typedef M MutexT
 Mutex type More...
 
typedef S SharedState
 Thread shared state More...
 
typedef ThreadGroup< T, S, M > This
 This type. More...
 
typedef Thread::Init ThreadInit
 Thread init type More...
 

Public Member Functions

 ThreadGroup ()
 Default constructor. More...
 
 ThreadGroup (Thread::Func func)
 Constructor for function-based thread. More...
 
virtual ~ThreadGroup ()
 Destructor. More...
 
bool active () const
 Get whether thread group is active (threads running). More...
 
Thiscancel ()
 Set cancel flags to signal all threads to stop. More...
 
bool cancelled () const
 Get cancel flag. More...
 
bool join ()
 Join all threads by waiting for them to stop. More...
 
ulong size () const
 Get thread group size. More...
 
bool start (uint count=1)
 Create new threads, add to group and start them. More...
 

Public Attributes

SharedState shared_state
 Shared state used by threads. More...
 
ThreadInit thread_init
 Thread init values for function-based threads, not used for class-based threads More...
 

Protected Types

typedef impl::ThreadGroupNode< S, T > Node
 

Protected Attributes

bool active_
 
bool cancel_flag_
 
Nodefirst_
 
Nodelast_
 
MutexT mutex_
 
ulong size_
 

Member Typedef Documentation

◆ MutexT

typedef M MutexT

Mutex type

◆ Node

typedef impl::ThreadGroupNode<S,T> Node
protected

◆ SharedState

typedef S SharedState

Thread shared state

◆ This

typedef ThreadGroup<T,S,M> This

This type.

◆ ThreadInit

Thread init type

Constructor & Destructor Documentation

◆ ThreadGroup() [1/2]

ThreadGroup ( )
inline

Default constructor.

◆ ThreadGroup() [2/2]

ThreadGroup ( Thread::Func  func)
inline

Constructor for function-based thread.

Parameters
funcThread function pointer

◆ ~ThreadGroup()

virtual ~ThreadGroup ( )
inlinevirtual

Destructor.

Member Function Documentation

◆ active()

bool active ( ) const
inline

Get whether thread group is active (threads running).

  • The group is active when at least 1 thread is started, and inactive once join() returns
  • The group does not become inactive until join() is called, even if all threads finished
Returns
Whether active

◆ cancel()

This& cancel ( )
inline

Set cancel flags to signal all threads to stop.

  • Ignored if using function-based threads
Returns
This

◆ cancelled()

bool cancelled ( ) const
inline

Get cancel flag.

  • This flag is set when active and cancel() is called to stop threads
  • This flag is reset by join()
Returns
Whether threads are cancelled

◆ join()

bool join ( )
inline

Join all threads by waiting for them to stop.

Returns
Always true

◆ size()

ulong size ( ) const
inline

Get thread group size.

Returns
Number of threads running

◆ start()

bool start ( uint  count = 1)
inline

Create new threads, add to group and start them.

  • This may be called multiple times to grow the number of threads
  • This fails once cancel() is called, until join() is called to reset
  • Fails for function-based threads if thread_init.func isn't set (by constructor or directly)
Parameters
countNumber of threads to add and start
Returns
Whether successful, false on error

Member Data Documentation

◆ active_

bool active_
protected

◆ cancel_flag_

bool cancel_flag_
protected

◆ first_

Node* first_
protected

◆ last_

Node* last_
protected

◆ mutex_

MutexT mutex_
mutableprotected

◆ shared_state

SharedState shared_state

Shared state used by threads.

◆ size_

ulong size_
protected

◆ thread_init

ThreadInit thread_init

Thread init values for function-based threads, not used for class-based threads


The documentation for this struct was generated from the following file: