Evo C++ Library v0.5.1
Public Types | Public Member Functions | Public Attributes | Protected Types | Protected Attributes | List of all members
EventThreadPool Class Reference

#include <evo/event_thread.h>

Inheritance diagram for EventThreadPool:
Inheritance graph
[legend]

Detailed Description

Event processing thread pool.

Example
using namespace evo;
struct MyEvent : Event {
bool operator()() {
// ...
return true;
}
};
int main() {
// Setup
EventThreadPool threadpool;
threadpool.start(2);
// Add an event to invoke in thread pool
threadpool.add(new MyEvent);
// Shutdown
threadpool.shutdown().join();
return 0;
}

Public Types

typedef MutexInert MutexT
 Mutex type More...
 
typedef EventThreadState SharedState
 Thread shared state More...
 
typedef EventThreadPool This
 This type. More...
 
typedef Thread::Init ThreadInit
 Thread init type More...
 

Public Member Functions

 EventThreadPool (ulong wait_timeout_ms=1)
 Constructor. More...
 
bool active () const
 Get whether thread group is active (threads running). More...
 
Thisadd (Event *event, ulongl spinwait_ns=1)
 Add an event to queue to be processed. More...
 
bool join ()
 Join all threads by waiting for them to stop. More...
 
Thisshutdown ()
 Stop processing events and shutdown threads. 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< EventThreadState, ThreadNode
 

Protected Attributes

bool active_
 
bool cancel_flag_
 
Nodefirst_
 
Nodelast_
 
MutexT mutex_
 
ulong size_
 

Member Typedef Documentation

◆ MutexT

typedef MutexInert MutexT
inherited

Mutex type

◆ Node

typedef impl::ThreadGroupNode<EventThreadState ,Thread > Node
protectedinherited

◆ SharedState

typedef EventThreadState SharedState
inherited

Thread shared state

◆ This

This type.

◆ ThreadInit

typedef Thread::Init ThreadInit
inherited

Thread init type

Constructor & Destructor Documentation

◆ EventThreadPool()

EventThreadPool ( ulong  wait_timeout_ms = 1)
inline

Constructor.

Parameters
wait_timeout_msMaximum thread wait while idle – see waitms in EventQueue::process_multiwait()

Member Function Documentation

◆ active()

bool active ( ) const
inlineinherited

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

◆ add()

This& add ( Event event,
ulongl  spinwait_ns = 1 
)
inline

Add an event to queue to be processed.

  • This takes ownership of the event pointer, and will free it once the event is completed (via C++ delete operator)
    • The event is only freed if it returns true, otherwise it's assumed that ownership was transferred elsewhere
  • This blocks while queue is full (spin-wait with 1 nanosecond sleep) – a full queue should be avoided
  • Caution: Do not call from the same queue that invoked (called) the event, this will deadlock if the queue is full
Parameters
eventEvent pointer to add and take ownership of
spinwait_nsSpin-wait sleep time in nanoseconds (usually default is preferred) – used to sleep each loop while spin waiting
Returns
This

◆ join()

bool join ( )
inlineinherited

Join all threads by waiting for them to stop.

Returns
Always true

◆ shutdown()

This& shutdown ( )
inline

Stop processing events and shutdown threads.

  • This starts the shutdown process, call join() to wait for all threads to fully stop
  • Stop adding events to the queue before calling this, otherwise behavior is undefned (likely shutdown delay or memory leak)
Returns
This

◆ size()

ulong size ( ) const
inlineinherited

Get thread group size.

Returns
Number of threads running

◆ start()

bool start ( uint  count = 1)
inlineinherited

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_
protectedinherited

◆ cancel_flag_

bool cancel_flag_
protectedinherited

◆ first_

Node* first_
protectedinherited

◆ last_

Node* last_
protectedinherited

◆ mutex_

MutexT mutex_
mutableprotectedinherited

◆ shared_state

SharedState shared_state
inherited

Shared state used by threads.

◆ size_

ulong size_
protectedinherited

◆ thread_init

ThreadInit thread_init
inherited

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


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