Skip to content

Commit 4143f08

Browse files
authored
Update CacheManager.cpp
1 parent 7c96a10 commit 4143f08

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/CacheManager.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ void CacheManager::accessCache(int key, int value) {
1919
}
2020
}
2121

22-
void CacheManager::switchPolicy() {
23-
while (true) {
24-
std::this_thread::sleep_for(std::chrono::seconds(5));
22+
// Modified to use the stop flag
23+
void CacheManager::switchPolicy(std::atomic<bool>& stop_flag) {
24+
// Loop only until the flag is set to true
25+
while (!stop_flag.load()) {
26+
std::this_thread::sleep_for(std::chrono::seconds(2)); // Shortened for test
27+
28+
// Check the flag again in case it was set while sleeping
29+
if (stop_flag.load()) break;
30+
2531
setPolicy(current_policy == LFU ? LRU : LFU);
2632
std::cout << "Cache policy switched!" << std::endl;
2733
}

0 commit comments

Comments
 (0)