Worker CPU thread for linux working

This commit is contained in:
Ben
2018-12-07 17:02:44 +00:00
parent 5dc1d87bf6
commit 0ccefcbfab
5 changed files with 39 additions and 27 deletions

View File

@@ -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)

Binary file not shown.

View File

@@ -2,20 +2,22 @@
#include <atomic>
#include <thread>
#include <mutex>
class CPU {
public:
CPU();
std::atomic<int> CPU_PREVIOUS_IDLE;
std::atomic<int> CPU_PREVIOUS_TOTAL;
std::atomic<int> CPU_IDLE;
std::atomic<int> CPU_TOTAL;
std::mutex CPU_Mutex;
int CPU_PREVIOUS_IDLE;
int CPU_PREVIOUS_TOTAL;
int CPU_IDLE;
int CPU_TOTAL;
std::atomic<int> 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();
@@ -23,16 +25,7 @@ public:
virtual ~CPU();
private:
std::thread* m_pollThread;
std::atomic<bool> 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;

View File

@@ -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;
}

View File

@@ -1,12 +1,17 @@
#include <iostream>
#include <cpu.h>
#include <unistd.h>
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);
}
}