C++/Thread

Thread 기초

(26010)

두개의 스레드를 만들어서 실행한다.
하나의 스레드는 0.1초에 한번씩 10번을 "Apple" 이라고 출력하고,

다른 스레드는 0.2초에 한 번 씩 5번을 "Banana" 라고 출력한다.


#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
void apple(){
        for(auto i=1;i<=10;i++){
                usleep(1000000);
                cout<<"Apple"<<endl;
        }
}
void banana(){
        for(auto i=1;i<=5;i++){
                usleep(200000);
                cout<<"Banana"<<endl;
        }
}
int main(){
        std::thread first(apple);
        std::thread second(banana);
        first.join();
        second.join();
        cout<<"END"<<endl;
        return 0;
}

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

클래스로 Thread 구현  (0) 2021.01.27