Group
Real-Time Programming in C++
Objective
Write a C++ program that simulates a task scheduling system. The program should allow tasks to be scheduled at specific intervals and execute them accordingly. You can simulate different tasks, such as displaying messages or performing simple actions, and schedule them to run at specified times or intervals. Use a loop to monitor the task schedule and execute tasks when their scheduled time arrives.
Implement a task scheduling system.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the input-output stream library
#include <thread> // Include the thread library for time management
#include <chrono> // Include the chrono library for handling time intervals
using namespace std;
// A simple task function to simulate a scheduled task
void task(string taskName) {
cout << taskName << " task executed!" << endl; // Print a message indicating the task is executed
}
// Function to schedule and manage tasks
void scheduleTasks() {
// Schedule tasks with different time intervals
this_thread::sleep_for(chrono::seconds(3)); // Wait for 3 seconds
task("Task 1"); // Execute Task 1 after 3 seconds
this_thread::sleep_for(chrono::seconds(2)); // Wait for 2 seconds
task("Task 2"); // Execute Task 2 after 2 more seconds
this_thread::sleep_for(chrono::seconds(4)); // Wait for 4 seconds
task("Task 3"); // Execute Task 3 after 4 more seconds
this_thread::sleep_for(chrono::seconds(1)); // Wait for 1 second
task("Task 4"); // Execute Task 4 after 1 more second
}
int main() {
// Start the task scheduling system
cout << "Scheduling tasks..." << endl;
scheduleTasks(); // Call the scheduleTasks function to manage task execution
return 0;
}
Output
Scheduling tasks...
Task 1 task executed!
Task 2 task executed!
Task 3 task executed!
Task 4 task executed!