Demonstrates thread-safe pub/sub messaging with simple_event_bus.
Demonstrates thread-safe pub/sub messaging with simple_event_bus.Shows event publishing, subscription, unsubscription, priority ordering, and error callbacks.
- See also
- kcenon::common::simple_event_bus
#include <iostream>
#include <string>
{
};
{
};
{
};
{
std::cout << "=== Event Bus Example ===\n\n";
std::cout << "1. Subscribing to events...\n";
{ std::cout << " [Auth] User '" << e.username << "' logged in from " << e.ip_address << "\n"; });
{ std::cout << " [Audit] Login recorded for '" << e.username << "'\n"; });
{ std::cout << " [Orders] Order #" << e.order_id << " placed, total: $" << e.total << "\n"; });
bus.set_error_callback([](const std::string& error, size_t, uint64_t)
{ std::cerr <<
" [Error] Event bus error: " <<
error <<
"\n"; });
std::cout << "\n2. Publishing events...\n";
std::cout << "\n3. Unsubscribing audit logger...\n";
bus.unsubscribe(audit_sub);
std::cout << "\n4. Publishing after unsubscribe...\n";
std::cout << "\n5. Priority-ordered publishing...\n";
{ std::cout << " [Alert] severity=" << e.severity << ": " << e.message << "\n"; });
bus.publish(
system_alert{
"CPU usage high", 2}, event_priority::high);
bus.publish(
system_alert{
"Disk space low", 1}, event_priority::normal);
bus.unsubscribe(login_sub);
bus.unsubscribe(order_sub);
bus.unsubscribe(alert_sub);
std::cout << "\nDone.\n";
return 0;
}
Event bus abstraction and common events.
simple_event_bus & get_event_bus()
Access the global event bus instance.