Real-Time Event-Based Notification System in C++

In this exercise, you will develop a C++ program that simulates a real-time notification system based on events. The system will monitor specific events, such as changes in the status of an application or environment, and notify the user whenever an event occurs. The program will simulate event detection and notification handling, using a loop to check for events and send notifications in real-time.

Group

Real-Time Programming in C++

Objective

Write a C++ program that creates a real-time event-based notification system. The program should monitor specific events and notify the user when an event occurs. Use a loop to simulate the detection of events and trigger notifications. You may implement different types of events such as status changes, environmental conditions, or other triggers that require immediate attention.

Create a real-time event-based notification system.

Example C++ Exercise

 Copy C++ Code
#include <iostream>   // Include the input-output stream library
#include <thread>      // Include the thread library to simulate event delays
#include <chrono>      // Include the chrono library to manage time intervals
using namespace std;

// Function to simulate real-time event notification
void eventNotificationSystem() {
    int eventCount = 0;  // Variable to keep track of the number of events

    while (true) {  // Infinite loop to simulate continuous event monitoring
        // Simulate detecting an event (this can be a random event in real systems)
        eventCount++;

        // Simulate a delay before triggering the event notification
        this_thread::sleep_for(chrono::seconds(3));  // Event detection interval of 3 seconds

        // Notify the user about the detected event
        cout << "Event #" << eventCount << " detected! Notifying user..." << endl;

        // Simulate the process of notifying the user (this can be an actual notification in a real system)
        this_thread::sleep_for(chrono::seconds(1));  // Simulate the time it takes to notify the user

        cout << "Notification sent to user for Event #" << eventCount << endl;

        // Simulate a break or delay between events for testing purposes
        if (eventCount == 5) {
            break;  // Stop after 5 events for testing
        }
    }
}

int main() {
    // Start the event notification system
    eventNotificationSystem();

    return 0;
}

 Output

Event #1 detected! Notifying user...
Notification sent to user for Event #1
Event #2 detected! Notifying user...
Notification sent to user for Event #2
Event #3 detected! Notifying user...
Notification sent to user for Event #3
Event #4 detected! Notifying user...
Notification sent to user for Event #4
Event #5 detected! Notifying user...
Notification sent to user for Event #5

Share this C++ Exercise


More C++ Programming Exercises of Real-Time Programming in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.