File tree Expand file tree Collapse file tree 1 file changed +18
-4
lines changed
Expand file tree Collapse file tree 1 file changed +18
-4
lines changed Original file line number Diff line number Diff line change 11#include " CacheManager.h"
22#include < iostream>
33#include < thread>
4+ #include < atomic>
5+ #include < chrono>
46
57int 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}
You can’t perform that action at this time.
0 commit comments