C++11의 thread 생성, sleep, join, joinable 기본 예제


join()은 thread가 종료될 때까지 기다리는 함수이고,

joinable()은 join()으로 기다릴 수 있는 상태인지 ( = thread가 돌고 있는 상태인지? 아니면 종료된 상태인지)를 확인하는 함수 입니다.



#include <iostream>

#include <thread>


void func1(void)

{

    std::cout << "func1 start" << std::endl;


    for (int i=0; i<3; i++)

    {

        std::cout << "func1 : " << i << std::endl;

        std::this_thread::sleep_for( std::chrono::seconds(1) );

    }

}


void func2(int count)

{

    std::cout << "func1 start" << std::endl;


    for (int i=0; i<count; i++)

    {

        std::cout << "func2 : " << i << std::endl;

        std::this_thread::sleep_for( std::chrono::milliseconds(1) );

    }

}


int main(int argc, char *argv[])

{

    std::cout << "main start" << std::endl;


    std::thread thread1(&func1);

    thread1.join();

    std::cout << "thread1 end" << std::endl;


    std::thread thread2(&func2, 5);

    thread2.join();

    std::cout << "thread2 end" << std::endl;


    return 0;

}


예제 실행 결과는 다음과 같습니다.





반응형

'아무거나' 카테고리의 다른 글

왕십리 CGV 골드 클래스  (0) 2018.07.15
대선 득표율  (0) 2017.05.10
펀토리하우스  (0) 2017.03.18
대선 후보 지지율  (0) 2017.03.10
마이크로소프트웨어 (마소) 복간  (1) 2017.03.08

+ Recent posts