63 auto now = chrono::system_clock::now();
64 auto ms = chrono::duration_cast<chrono::milliseconds>(
65 now.time_since_epoch()) % 1000;
98 cout <<
"\n=== Demo 1: Simple Timed Event ===" <<
endl;
99 cout <<
"[" <<
now_str() <<
"] Scheduling event for 500ms from now..." <<
endl;
105 this_thread::sleep_for(chrono::milliseconds(700));
131 cout <<
"\n=== Demo 2: Multiple Events Execute in Time Order ===" <<
endl;
133 atomic<bool> flags[4] = {
false,
false,
false,
false};
136 cout <<
"[" <<
now_str() <<
"] Scheduling events out of order..." <<
endl;
148 cout <<
" Events scheduled: #3 @300ms, #1 @100ms, #4 @400ms, #2 @200ms" <<
endl;
149 cout <<
" Expected order: 1, 2, 3, 4" <<
endl;
152 this_thread::sleep_for(chrono::milliseconds(600));
182 cout <<
"\n=== Demo 3: Event Cancellation ===" <<
endl;
190 cout <<
"[" <<
now_str() <<
"] Scheduled 'KEEP' @200ms and 'CANCEL' @300ms" <<
endl;
193 this_thread::sleep_for(chrono::milliseconds(50));
195 cout <<
"[" <<
now_str() <<
"] Canceled 'CANCEL' event: "
197 cout <<
" Event status: "
199 ?
"Canceled" :
"Other") <<
endl;
202 this_thread::sleep_for(chrono::milliseconds(400));
203 cout <<
" Only 'KEEP' should have executed above." <<
endl;
234 cout <<
"\n=== Demo 4: Event Rescheduling ===" <<
endl;
238 cout <<
"[" <<
now_str() <<
"] Originally scheduled for 500ms" <<
endl;
242 this_thread::sleep_for(chrono::milliseconds(100));
243 cout <<
"[" <<
now_str() <<
"] Rescheduling to 100ms from now (200ms total)" <<
endl;
246 this_thread::sleep_for(chrono::milliseconds(300));
247 cout <<
" Event executed early due to rescheduling." <<
endl;
284 cout <<
"\n=== Demo 5: Self-Rescheduling Periodic Event ===" <<
endl;
289 cout <<
"[" <<
now_str() <<
"] Starting periodic event (4 ticks, 150ms interval)" <<
endl;
293 this_thread::sleep_for(chrono::milliseconds(800));
319 cout <<
"\n=== Demo 6: Event with Lambda Callback ===" <<
endl;
325 cout <<
"[" <<
now_str() <<
"] Lambda callback 1, counter = " << counter <<
endl;
330 cout <<
"[" <<
now_str() <<
"] Lambda callback 2, counter = " << counter <<
endl;
336 this_thread::sleep_for(chrono::milliseconds(400));
338 cout <<
" Final counter value: " << counter <<
endl;
351 "Out_Queue",
"In_Queue",
"Canceled",
"Executing",
"Executed",
"To_Delete",
"Deleted"
353 cout <<
" " << name <<
" status: "
359 cout <<
"\n=== Demo 7: Event Lifecycle Status ===" <<
endl;
370 this_thread::sleep_for(chrono::milliseconds(300));
383 cout <<
"\n=== Demo 8: Cancel and Delete Event ===" <<
endl;
387 cout <<
"[" <<
now_str() <<
"] Event pointer before: " <<
event <<
endl;
390 this_thread::sleep_for(chrono::milliseconds(100));
393 cout <<
"[" <<
now_str() <<
"] Event pointer after cancel_delete: " <<
event <<
endl;
394 cout <<
" (Should be nullptr - memory freed by cancel_delete_event)" <<
endl;
403 cout <<
"========================================" <<
endl;
404 cout <<
" TimeoutQueue Example Demonstrations " <<
endl;
405 cout <<
"========================================" <<
endl;
420 cout <<
"\n=== Shutdown ===" <<
endl;
425 this_thread::sleep_for(chrono::milliseconds(100));
428 catch (
const exception& e)
430 cerr <<
"Error: " << e.what() <<
endl;
435 cout <<
"\n========================================" <<
endl;
436 cout <<
" All demonstrations completed! " <<
endl;
437 cout <<
"========================================" <<
endl;
Time time_plus_msec(const Time ¤t_time, const int &msec)
function< void()> callback
void EventFct() override
Event handler function to be overridden.
CallbackEvent(const Time &t, function< void()> cb)
void EventFct() override
Event handler function to be overridden.
CancellableEvent(const Time &t, const string &n)
const string & get_name() const
NumberedEvent(const Time &t, int num, atomic< bool > &flag)
void EventFct() override
Event handler function to be overridden.
atomic< bool > & executed_flag
void EventFct() override
Event handler function to be overridden.
PeriodicEvent(const Time &t, TimeoutQueue *q, const string &n, int interval, int max_exec)
ReschedulableEvent(const Time &t, const string &n)
void EventFct() override
Event handler function to be overridden.
void EventFct() override
Event handler function to be overridden.
SimpleEvent(const Time &t, const string &msg)
Base class for scheduled events.
Event(const Time &t, std::string name="")
@ Canceled
Removed from queue before execution.
Execution_Status get_execution_status() const
Thread-safe priority queue for scheduling timed events.
void reschedule_event(const Time &trigger_time, Event *event)
Reschedule an event to a new time.
void schedule_event(const Time &trigger_time, Event *)
Schedule an event at a specific time.
void cancel_delete_event(Event *&event)
Cancel and delete an event.
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.
Priority queue for scheduling timed events.
void demo_rescheduling(TimeoutQueue &queue)
static string format_time(const Time &t)
void print_status(const TimeoutQueue::Event *e, const string &name)
void demo_periodic_event(TimeoutQueue &queue)
void demo_event_status(TimeoutQueue &queue)
void demo_cancellation(TimeoutQueue &queue)
void demo_callback_event(TimeoutQueue &queue)
void demo_priority_ordering(TimeoutQueue &queue)
void demo_cancel_delete(TimeoutQueue &queue)
void demo_simple_event(TimeoutQueue &queue)
static Time time_from_now_ms(int ms)