|
Event-Driven Programming in C++ 1.0
A case study on event-driven programming in C++
|
A thread-safe event queue. More...
#include <event_queue.hpp>
Public Types | |
| using | Event = std::function< void()> |
| Type alias for an event. | |
Public Member Functions | |
| void | pushEvent (const Event &event) |
| Enqueues an event. | |
| void | processEvents () |
| Processes all events in the queue. | |
| bool | isEmpty () const |
| Checks whether the event queue is empty. | |
Private Attributes | |
| std::queue< Event > | events_ |
| The underlying queue storing events. | |
| std::mutex | mutex_ |
| Mutex to protect access to the event queue. | |
A thread-safe event queue.
The EventQueue class allows you to enqueue events (callable objects) and later process them sequentially (in a FIFO manner). It uses a mutex to ensure that operations on the queue are safe across multiple threads.
| using event_queue::EventQueue::Event = std::function<void()> |
Type alias for an event.
An event is defined as a callable object that takes no arguments and returns void.
| bool event_queue::EventQueue::isEmpty | ( | ) | const |
Checks whether the event queue is empty.
| void event_queue::EventQueue::processEvents | ( | ) |
Processes all events in the queue.
Executes all events in the queue in FIFO order. After processing, the queue is empty. This operation is thread-safe.
| void event_queue::EventQueue::pushEvent | ( | const Event & | event | ) |
Enqueues an event.
Adds an event to the queue. The event will be executed when processEvents() is called.
| event | The event to enqueue. |
|
private |
The underlying queue storing events.
|
mutableprivate |
Mutex to protect access to the event queue.