diff --git a/CMakeLists.txt b/CMakeLists.txt index da01074..e6d833b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set(LinuxDep ${Platform}/linux) set(Include ./include) +set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) include_directories(${Build}/ ${Include}) @@ -42,4 +43,6 @@ add_executable(${Build} ${PlatformDepSource} ) +target_link_libraries(${Build} ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(${Build} pthread) target_link_libraries(${Build} Threads::Threads) diff --git a/bin/resource-monitor b/bin/resource-monitor index 3c3b10e..545aa94 100755 Binary files a/bin/resource-monitor and b/bin/resource-monitor differ diff --git a/include/cpu.h b/include/cpu.h index e00d781..51ac75d 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -2,37 +2,30 @@ #include #include +#include class CPU { public: CPU(); - std::atomic CPU_PREVIOUS_IDLE; - std::atomic CPU_PREVIOUS_TOTAL; - std::atomic CPU_IDLE; - std::atomic CPU_TOTAL; + std::mutex CPU_Mutex; + int CPU_PREVIOUS_IDLE; + int CPU_PREVIOUS_TOTAL; + int CPU_IDLE; + int CPU_TOTAL; - std::atomic UPDATE_INTERVAL; // ms + int UPDATE_INTERVAL; // s void START_CPU_POLLING(); - static void CPU_POLL(); + static void CPU_POLL(CPU* cpu); void END_CPU_POLLING(); double CPU_PERCENT(); - virtual ~CPU(); + virtual ~CPU(); private: std::thread* m_pollThread; - std::atomic m_isPolling; + bool m_isPolling; }; -static CPU* instance; -static CPU* GetCPUInstance() { - if (instance == (void*)0) { - CPU cpu; - instance = &cpu; - return instance; - } - return instance; -} - +static CPU* CPU_Instance; diff --git a/platform/linux/monitoring/cpu.cpp b/platform/linux/monitoring/cpu.cpp index 3aec16b..cc1a537 100644 --- a/platform/linux/monitoring/cpu.cpp +++ b/platform/linux/monitoring/cpu.cpp @@ -9,26 +9,33 @@ CPU::CPU() { this->UPDATE_INTERVAL = 1000; this->m_isPolling = false; + std::cout << "CPU Class Instantated" << std::endl; } void CPU::START_CPU_POLLING() { if (this->m_isPolling) { return; } - std::thread t(&CPU::CPU_POLL); - this->m_pollThread = &t; this->m_isPolling = true; + std::thread* t = new std::thread(&CPU::CPU_POLL, this); + this->m_pollThread = t; std::cout << "Worker CPU thread started" << std::endl; } -void CPU::CPU_POLL() { - CPU* cpu = GetCPUInstance(); - while (cpu->m_isPolling) { +void CPU::CPU_POLL(CPU* cpu) { + std::cout << "From CPU Thread Start" << std::endl; + while (true) { + cpu->CPU_Mutex.lock(); + if (!cpu->m_isPolling) { + cpu->CPU_Mutex.unlock(); + return; + } cpu->CPU_PREVIOUS_IDLE++; cpu->CPU_PREVIOUS_TOTAL++; cpu->CPU_IDLE++; cpu->CPU_TOTAL++; - usleep(cpu->UPDATE_INTERVAL); + cpu->CPU_Mutex.unlock(); + sleep(1); } } @@ -37,15 +44,19 @@ void CPU::END_CPU_POLLING() { return; } this->m_isPolling = false; - usleep(this->UPDATE_INTERVAL + 5); m_pollThread->join(); delete m_pollThread; } double CPU::CPU_PERCENT() { + this->CPU_Mutex.lock(); + + this->CPU_Mutex.unlock(); return (double)this->CPU_TOTAL; } CPU::~CPU() { - CPU::m_pollThread->~thread(); + this->m_isPolling = false; + m_pollThread->join(); + delete m_pollThread; } diff --git a/src/main.cpp b/src/main.cpp index ac6fd0d..8eb5e17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,17 @@ #include #include +#include int main(int argc, char** argv) { - CPU* cpu = GetCPUInstance(); + CPU* cpu = new CPU(); + CPU_Instance = cpu; cpu->START_CPU_POLLING(); + usleep(1000000); while(1) { std::cout << cpu->CPU_PERCENT() << std::endl; + + sleep(1); } }