본문 바로가기
시스템개발/cplusplus

[비동기] Async 사용 1

by 이노키_ 2022. 6. 16.

Async를 사용하는 방법

#include <iostream>
#include <future>

using namespace std::chrono_literals;

int fn() {
    std::this_thread::sleep_for(1s);
    std::cout << "async fn is called" << std::endl;
    return 42;
}

int main()
{
    //task기반의 비동기 처리
    std::future<int> fut = std::async(std::launch::async, fn);
    int num = fut.get();
    std::cout << "Hello World : " << num << std::endl;
}

결과값

async fn is called
Hello World : 42

댓글