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
- gui
- Module.symvers
- .config
- stack
- 라즈베리파이3 라즈비안
- 일곱 난쟁이
- dfs recursive
- 모듈
- AWS
- 라즈베리파이 uboot
- 백준
- 한수
- u-boot
- DFS
- putty
- 라즈베리파이3 ftfp
- tftp 서버
- GPIO
- 2309
- deque
- STL deque
- tftp-hpa
- 인접행렬
- uboot
- 최단경로
- 디바이스 드라이버
- 라즈비안
- 라즈베리파이3
- tftp
- 디바이스드라이버
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