-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutlithreaded task timer.cpp
More file actions
48 lines (36 loc) · 1.43 KB
/
mutlithreaded task timer.cpp
File metadata and controls
48 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
using namespace std;
// Function that simulates a timer for a given task
void taskTimer(string taskName, int seconds) {
while (seconds > 0) {
cout << taskName << ": " << seconds << " seconds remaining\n";
this_thread::sleep_for(chrono::seconds(1));
--seconds;//as time is decreasing
}
cout << " Task " << taskName << " completed.\n";
}
int main() {
int numTasks;
cout << "Enter number of tasks: ";
cin >> numTasks;
vector<thread> taskThreads;
for (int i = 0; i < numTasks; ++i) {
string name;
int timeSec;
cout << "Enter name for task " << (i + 1) << ": ";//because i starts from 0
cin >> name;
cout << "Enter duration in seconds for task \"" << name << "\": ";
cin >> timeSec;
// Start a thread for each task
taskThreads.emplace_back(taskTimer, name, timeSec);//emplace_back efficiency is better than push_back as it constructs an object in place inside the container
}
// Join all threads
for (auto& t : taskThreads) {// For each thread t in the vector taskThreads, wait (join) until it's finished before continuing. This ensures that the main function waits for all tasks to complete before printing the final message,we used by reference because thread objects
t.join();
}
cout << "All tasks completed.\n";
return 0;
}