달공이와 임베디드

[C++] Class-based Thread &Timer 본문

C++

[C++] Class-based Thread &Timer

하일리99 2020. 2. 17. 21:55

 


#include <iostream>
#include <thread>
#include <chrono>
#include <vector>

using namespace std;

class GuiView;

class GUIViewManager
{
private:
    static vector<GuiView*> gui_list;
    static int gui_update_interval_milliseconds; 

    GUIViewManager(int _gui_update_interval_milliseconds = 10) : gui_update_interval_milliseconds(_gui_update_interval_milliseconds){}
    ~GUIViewManager();
    
public:
    static GUIViewManager* getInstance()
    {
        static GUIViewManager instance;
        return &instance;
    }

    void push(GuiView* view)
    {
        gui_list.push_back(view);
    }

    void set_gui_update_interval_milliseconds(int milliseconds)
    {
        gui_update_interval_milliseconds = milliseconds;
    }

    static void listen_state_changed()
    {
        while(1)
        {
            for(auto iter = gui_list.begin(); iter != gui_list.end(); iter++)
            {
                if ((*iter)->isChangeStated)
                {   
                    (*iter)->update();
                }
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(gui_update_interval_milliseconds));
        }
    }
};

class GuiView
{
public:
    bool isChangeStated;
    thread update_gui;
    GuiView() : isChangeStated(false)
    {
        GUIViewManager::getInstance()->push(this);
        cout << "GUIViewManager::getInstance()->push(this);" << endl;
    }
    virtual void update();
};

class LED : GuiView
{   
private:
    bool isLEDon;
public:
    LED(bool _isLEDon) : isLEDon(_isLEDon){}
    void update();
};


void LED::update()
{
    cout << (isLEDon ? "LED is ON" : "LED is OFF") << endl; 
}


class Background : GuiView
{   
private:
    string color;
public:
    Background(string _color) : color(_color){}
    void update();
    string get_color(){return color;}
};


void Background::update()
{
    cout << "Background color :"  << get_color() << endl; 
}


class Timer
{
private:
    int m_milliseconds;
    void timer_routine();
    thread timer_thread;
public:
    bool isTimerRun;
    Timer(): isTimerRun(false){}
    void setInterval(int milliseconds = 1000){m_milliseconds = milliseconds;}
    void start();
    void finished();
    void stop();
};

void Timer::timer_routine()
{
    while(1)
    {
        if (!isTimerRun) break;
        this_thread::sleep_for(chrono::milliseconds(m_milliseconds));
        this->finished();
    }
    // if isTimerRun is true, timer_thread is terminated. 
}

void Timer::start()
{
    isTimerRun = true;
    timer_thread = thread(timer_routine);
}

void Timer::stop()
{
    isTimerRun = false;
}

void Timer::finished()
{
    // do somethins periodically
    static int time = 0;
    cout << "[system timer] " << time << " sec" << endl;
}

void run_gui_thread()
{
    Background bg("Red");
    LED led(true);
    GUIViewManager::getInstance->listen_state_changed();
}

void run_state_thread()
{
    Timer state_timer;
    state_timer.setInterval(1000);
    state_timer.start();
}

int main()
{
    thread gui_thread(run_gui_thread);
    thread state_thread(run_state_thread);
    gui_thread.join();
    state_thread.join();
}

'C++' 카테고리의 다른 글

[C++] Thread-based Update  (0) 2020.02.17
Comments