Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
timeoutQueue.H File Reference

Priority queue for scheduling timed events. More...

#include <unistd.h>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <chrono>
#include <functional>
#include <string>
#include <atomic>
#include <cstdint>
#include <ah-errors.H>
#include <tpl_dynMapTree.H>
#include <tpl_dynSetTree.H>
#include <tpl_binHeap.H>
#include <ah-time.H>
#include <utility>
Include dependency graph for timeoutQueue.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  TimeoutQueue
 Thread-safe priority queue for scheduling timed events. More...
 
class  TimeoutQueue::Event
 Base class for scheduled events. More...
 

Detailed Description

Priority queue for scheduling timed events.

This file provides a thread-safe priority queue for scheduling events that should be triggered at specific absolute times. It uses a background thread to monitor the queue and execute events when their trigger time arrives.

Features

  • Priority-based scheduling: Events ordered by trigger time using a binary heap
  • Thread-safe: Mutex-protected operations with condition variable signaling
  • Background execution: Dedicated thread monitors and triggers events
  • Event lifecycle: Supports scheduling, cancellation, rescheduling, and deletion
  • Status tracking: Events maintain execution status throughout their lifecycle

Event States

Events transition through these states:

  • Out_Queue: Not currently scheduled
  • In_Queue: Waiting for trigger time
  • Canceled: Removed before execution
  • Executing: Currently running EventFct()
  • Executed: Completed execution
  • To_Delete: Marked for cleanup
  • Deleted: Cleaned up

Usage Example

class MyEvent : public TimeoutQueue::Event {
public:
MyEvent(const Time& t) : Event(t) {}
void EventFct() override {
std::cout << "Event triggered!" << std::endl;
}
};
// Schedule event 5 seconds from now
Time trigger_time = current_time();
trigger_time.tv_sec += 5;
MyEvent* event = new MyEvent(trigger_time);
queue.schedule_event(trigger_time, event);
// Later: cancel if needed
queue.cancel_event(event);
// Cleanup
queue.shutdown();
struct timespec Time
Definition ah-time.H:50
Base class for scheduled events.
virtual void EventFct()=0
Event handler function to be overridden.
Thread-safe priority queue for scheduling timed events.
void schedule_event(const Time &trigger_time, Event *)
Schedule an event at a specific time.
void shutdown()
Shut down the queue and stop the background thread.
bool cancel_event(Event *event)
Cancel a scheduled event.
DynList< T > maps(const C &c, Op op)
Classic map operation.

Implementation Notes

  • Uses BinHeapVtl for efficient O(log n) priority queue operations
  • Background thread sleeps until next event or new scheduling
  • Condition variable wakes thread when queue is modified
See also
tpl_binHeap.H For the underlying priority queue
ah-time.H For time utilities
Note
Uses C++20 threading primitives (std::mutex, std::condition_variable, std::thread)
Author
Leandro Rabindranath León *

Definition in file timeoutQueue.H.