std::launch::async를 지정한 경우, 해당 함수를 즉시 호출한다.
#include <iostream>
#include <chrono>
#include <future>
string func(string str)
{
cout << str << '\n';
return "5초 끝";
}
int main()
{
cout << "시작" << '\n';
std::future<string> f1 = std::async(std::launch::async, func, "5초 세기 시작");
std::this_thread::sleep_for(std::chrono::seconds(5));
f1.get();
}
아래와 같이 출력된다.
시작
5초 세기 시작
(5초 후)
5초 끝
std::launch::deferred 를 지정한 경우, get()이나 wait()을 통해 함수가 실행된다.
...
string func(string str)
{
cout << str << '\n';
return "5초 끝";
}
int main()
{
std::cout << "시작" << '\n';
std::future<string> f1 = std::async(std::launch::deferred, func, "5초 세기 시작");
std::this_thread::wait_for(std::chrono::seconds(5));
std::cout << f1.get() << '\n';
}
아래와 같이 출력된다.
시작
(5초 후)
5초 세기 시작
5초 끝
'개인 공부 > C++' 카테고리의 다른 글
std::move (0) | 2023.05.15 |
---|---|
std::any_of (0) | 2023.05.15 |
std::find_if (0) | 2023.05.15 |
std::remove_if (0) | 2023.05.15 |
E1735 바깥쪽 함수의 지역 변수는 캡처 목록에 있지 않는 한 람다 본문에서 참조할 수 없습니다. (0) | 2022.11.21 |