#include <evo/event_thread.h>
Event processing thread pool.
- This runs a group of threads as EventQueue consumers that process events added to the queue
- Events are popped from the queue and invoked by a thread in the pool, then are destroyed (if event returns true)
- Use start() to start threads, and shutdown() and join() to shutdown
- Use add() to add events to queue
- Example
bool operator()() {
return true;
}
};
int main() {
threadpool.
add(
new MyEvent);
return 0;
}
◆ MutexT
◆ Node
◆ SharedState
◆ This
◆ ThreadInit
◆ EventThreadPool()
◆ active()
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
-
event | Event pointer to add and take ownership of |
spinwait_ns | Spin-wait sleep time in nanoseconds (usually default is preferred) – used to sleep each loop while spin waiting |
- Returns
- This
◆ join()
Join all threads by waiting for them to stop.
- Returns
- Always true
◆ shutdown()
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()
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
-
count | Number of threads to add and start |
- Returns
- Whether successful, false on error
◆ active_
◆ cancel_flag_
◆ first_
◆ last_
◆ mutex_
|
mutableprotectedinherited |
◆ shared_state
Shared state used by threads.
◆ size_
◆ thread_init
Thread init values for function-based threads, not used for class-based threads
The documentation for this class was generated from the following file: