Skip to content

Commit 312a534

Browse files
committed
set flag for enable and disable memory thread
1 parent 9f112cf commit 312a534

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

src/library/MemoryMonitor.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "MemoryMonitor.hpp"
22

3-
MemoryMonitor::MemoryMonitor(int durationTimeToCheckMS, bool &isMonitoringEnable)
4-
: durationTimeToCheckMS(durationTimeToCheckMS), isMonitoringEnable(isMonitoringEnable), lastMemoryUsage(0.0) {}
3+
MemoryMonitor::MemoryMonitor(int durationTimeToCheckMS)
4+
: durationTimeToCheckMS(durationTimeToCheckMS), lastMemoryUsage(0.0), monitoringMemoryStatus(false) {}
55

66
/**
77
* @brief Starts monitoring memory usage by launching a background thread.
@@ -30,6 +30,14 @@ MemoryMonitor::MemoryMonitor(int durationTimeToCheckMS, bool &isMonitoringEnable
3030
*/
3131
void MemoryMonitor::startMonitoring()
3232
{
33+
// if monitoring enabled
34+
if (this->monitoringMemoryStatus)
35+
{
36+
return;
37+
}
38+
39+
// if monitoring disabled
40+
this->monitoringMemoryStatus = true;
3341
monitorThread = std::thread(&MemoryMonitor::thread_getMemoryUsage, this);
3442
monitorThread.detach(); // Detach the thread so it runs in the background
3543
}
@@ -94,15 +102,8 @@ double MemoryMonitor::getLastMemoryUsage() const
94102
*/
95103
void MemoryMonitor::thread_getMemoryUsage()
96104
{
97-
while (true)
105+
while (this->monitoringMemoryStatus)
98106
{
99-
// if monitoring disable
100-
if (!this->isMonitoringEnable)
101-
{
102-
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
103-
continue;
104-
}
105-
106107
std::ifstream memInfoFile("/proc/meminfo");
107108
if (!memInfoFile.is_open())
108109
{
@@ -152,3 +153,20 @@ void MemoryMonitor::thread_getMemoryUsage()
152153
std::this_thread::sleep_for(std::chrono::milliseconds(this->durationTimeToCheckMS));
153154
}
154155
}
156+
157+
/**
158+
* @brief Stops the memory monitoring process.
159+
*
160+
* This function sets the `monitoringMemoryStatus` flag to `false`,
161+
* indicating that memory monitoring should be stopped. As a result,
162+
* any ongoing memory usage tracking will cease, and the system will
163+
* no longer collect or process memory usage data.
164+
*
165+
* It is essential to call this method when memory monitoring is
166+
* no longer required to free up resources and ensure the monitoring
167+
* process is gracefully terminated.
168+
*/
169+
void MemoryMonitor::stopMonitoring()
170+
{
171+
this->monitoringMemoryStatus = false;
172+
}

src/library/MemoryMonitor.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
class MemoryMonitor
1313
{
1414
public:
15-
MemoryMonitor(int durationTimeToCheckMS, bool &isMonitoringEnable);
15+
MemoryMonitor(int durationTimeToCheckMS);
1616
void startMonitoring();
17+
void stopMonitoring();
1718
double getLastMemoryUsage() const;
1819

1920
private:
2021
void thread_getMemoryUsage();
2122

23+
bool monitoringMemoryStatus;
2224
int durationTimeToCheckMS;
23-
bool &isMonitoringEnable;
2425
double lastMemoryUsage;
2526
std::thread monitorThread;
2627
};

0 commit comments

Comments
 (0)