달공이와 임베디드

[C++] Thread-based Update 본문

C++

[C++] Thread-based Update

하일리99 2020. 2. 17. 20:33

 


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


using namespace std;


bool isChangeStated = false;

void updateGui()
{
    isChangeStated = true;
}

void finishUpdateGui()
{
    isChangeStated = false;
}


void ShowLedState(bool& isLEDon)
{
    
    while(1)
    {
        if (isChangeStated)
        {
            finishUpdateGui();
            cout << (isLEDon ? "Led is ON" :  "Led is OFF") << endl;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }

}


void clock_timer(bool& isLEDon, int sec)
{
    thread update_gui;
    while(sec)
    {
        std::this_thread::sleep_for (std::chrono::seconds(1));
        isLEDon = !isLEDon;
        sec--;
        update_gui = thread(updateGui);
        update_gui.join();
    }
    
}

int main()
{
    bool isLEDon = true;
    bool prev_isLEDon = isLEDon;
    thread t1(clock_timer, std::ref(isLEDon), 10);
    thread t2(ShowLedState, std::ref(isLEDon));
    t1.join();
    t2.join();
}

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

[C++] Class-based Thread &Timer  (0) 2020.02.17
Comments