Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- u-boot
- GPIO
- uboot
- 백준
- dfs recursive
- 한수
- 라즈베리파이 uboot
- 2309
- 디바이스 드라이버
- tftp 서버
- 일곱 난쟁이
- 라즈베리파이3 ftfp
- stack
- deque
- tftp-hpa
- 디바이스드라이버
- DFS
- 모듈
- .config
- 최단경로
- AWS
- 인접행렬
- putty
- 라즈베리파이3
- STL deque
- 라즈비안
- 라즈베리파이3 라즈비안
- gui
- tftp
- Module.symvers
Archives
- Today
- Total
달공이와 임베디드
[C++] Class-based Thread &Timer 본문
#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