Skip to content

Commit 207b58d

Browse files
authored
Update main.cpp
1 parent 4143f08 commit 207b58d

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/main.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
#include "CacheManager.h"
22
#include <iostream>
33
#include <thread>
4+
#include <atomic>
5+
#include <chrono>
46

57
int main() {
68
CacheManager manager(5);
9+
std::atomic<bool> stop_flag(false); // The stop flag
710

8-
// Start a thread to periodically switch cache policies
9-
std::thread policySwitcher(&CacheManager::switchPolicy, &manager);
11+
// Pass a reference to the flag to the thread
12+
std::thread policySwitcher(&CacheManager::switchPolicy, &manager, std::ref(stop_flag));
1013

1114
// Simulate cache operations
12-
for (int i = 0; i < 100; i++) {
15+
std::cout << "Simulating cache access..." << std::endl;
16+
for (int i = 0; i < 20; i++) {
1317
manager.accessCache(i, i * 10);
18+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
1419
}
1520

16-
policySwitcher.join();
21+
// Let the policy switcher run a few times
22+
std::cout << "Running for 5 seconds before shutdown..." << std::endl;
23+
std::this_thread::sleep_for(std::chrono::seconds(5));
24+
25+
// --- Graceful Shutdown ---
26+
std::cout << "Signaling policy switcher to stop..." << std::endl;
27+
stop_flag.store(true); // Set the flag to true
28+
policySwitcher.join(); // Wait for the thread to finish its loop
29+
30+
std::cout << "Program finished successfully." << std::endl;
1731
return 0;
1832
}

0 commit comments

Comments
 (0)