launch — std::launch

launch — std::launch

std::launchstd::async가 실행하는 태스크의 실행 정책(launch policy)을 지정하는 열거형이에요. C++11에서 도입됐어요. <future> 헤더에 있어요.

BitmaskType 요구사항을 만족해요.

출처: cppreference

본문

// <future> 헤더, C++11
enum class launch : /* unspecified */ {
    async =    /* unspecified */,
    deferred = /* unspecified */,
    /* implementation-defined */
};

상수

상수 의미
async 새 스레드에서 비동기로 실행
deferred get()/wait() 호출 시까지 지연된 실행
`async deferred`

사용 예

#include <future>
#include <iostream>

// (1) async — 새 스레드
std::future<int> f1 =
    std::async(std::launch::async, [] { return 1; });

// (2) deferred — get() 시 실행
std::future<int> f2 =
    std::async(std::launch::deferred, [] { return 2; });

// (3) 구현이 선택
std::future<int> f3 = std::async([] { return 3; });

std::cout << f1.get() << f2.get() << f3.get();   // 123

deferred의 동작

std::future<int> f = std::async(std::launch::deferred, [] {
    std::cout << "runs on get()\n";
    return 42;
});
// 여기서는 아직 실행 안 됨
int v = f.get();   // 이 시점에 실행되어 42

특징

  • std::async의 첫 번째 인자로 정책을 줘요.
  • async | deferred가 기본이고, 구현이 메모리·리소스에 따라 선택해요.
  • deferred는 실행을 늦추고 싶을 때(쓸 결과만 계산) 유용해요.
// 둘을 함께 (OR)
auto f = std::async(std::launch::async | std::launch::deferred, work);

std::launch는 비동기 작업을 "즉시 새 스레드"로 실행할지, 아니면 "필요할 때 지연 실행"할지 제어하는 열거형이에요.

더 알아보기 (Learn more)

cppreference