본문으로 건너뛰기
Previous
Next
C++ async & launch | std::async·future·launch 정책 완벽 정리

C++ async & launch | std::async·future·launch 정책 완벽 정리

C++ async & launch | std::async·future·launch 정책 완벽 정리

이 글의 핵심

std::async는 함수를 비동기로 실행하고 future로 결과를 받는 C++11 API입니다. launch::async, launch::deferred 정책과 실전 예제를 정리합니다.

들어가며

std::async는 함수를 비동기로 실행하고 std::future로 결과를 받는 C++11 API입니다. std::thread보다 간결하며, launch 정책으로 실행 시점을 제어할 수 있습니다.

이 글을 읽으면

  • std::async로 비동기 작업을 실행하고 결과를 받습니다
  • launch::async, launch::deferred 정책의 차이를 이해합니다
  • 병렬 작업, 타임아웃, 예외 처리 패턴을 익힙니다
  • 실무에서 자주 쓰이는 비동기 패턴을 구현합니다

기본 개념

std::async란?

std::async는 함수를 비동기로 실행하고 std::future로 결과를 받습니다.

#include <future>
#include <iostream>
int compute(int x) {
    return x * x;
}
int main() {
    auto future = std::async(compute, 10);
    
    int result = future.get();  // 결과 대기
    std::cout << "결과: " << result << std::endl;  // 100
    
    return 0;
}

launch 정책

정책실행 시점스레드 생성사용 시나리오
launch::async즉시✅ 새 스레드CPU 집약적 작업
launch::deferredget() 호출 시❌ 현재 스레드조건부 실행
async | deferred구현 의존자동 선택일반적 사용 (기본)

실전 구현

1) 기본 사용

#include <future>
#include <iostream>
#include <thread>
#include <chrono>
int compute(int x) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return x * x;
}
int main() {
    auto future = std::async(compute, 10);
    
    std::cout << "계산 중..." << std::endl;
    
    int result = future.get();  // 대기
    std::cout << "결과: " << result << std::endl;  // 100
    
    return 0;
}

2) launch::async - 즉시 실행

#include <future>
#include <iostream>
#include <thread>
int main() {
    auto future = std::async(std::launch::async, []() {
        std::cout << "비동기 스레드 ID: " 
                  << std::this_thread::get_id() << std::endl;
        return 42;
    });
    
    std::cout << "메인 스레드 ID: " 
              << std::this_thread::get_id() << std::endl;
    
    int result = future.get();
    std::cout << "결과: " << result << std::endl;
    
    return 0;
}

출력:

메인 스레드 ID: 140735268771840
비동기 스레드 ID: 123145307557888
결과: 42

3) launch::deferred - 지연 실행

#include <future>
#include <iostream>
#include <thread>
int main() {
    auto future = std::async(std::launch::deferred, []() {
        std::cout << "지연 실행 스레드 ID: " 
                  << std::this_thread::get_id() << std::endl;
        return 42;
    });
    
    std::cout << "메인 스레드 ID: " 
              << std::this_thread::get_id() << std::endl;
    
    std::cout << "get 호출 전" << std::endl;
    int result = future.get();  // 이때 실행
    std::cout << "결과: " << result << std::endl;
    
    return 0;
}

출력:

메인 스레드 ID: 140735268771840
get 호출 전
지연 실행 스레드 ID: 140735268771840
결과: 42

주의: 같은 스레드에서 실행됨

4) 여러 비동기 작업

#include <future>
#include <iostream>
#include <thread>
#include <chrono>
int compute1() {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return 10;
}
int compute2() {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return 20;
}
int compute3() {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return 30;
}
int main() {
    auto start = std::chrono::high_resolution_clock::now();
    
    auto f1 = std::async(std::launch::async, compute1);
    auto f2 = std::async(std::launch::async, compute2);
    auto f3 = std::async(std::launch::async, compute3);
    
    int total = f1.get() + f2.get() + f3.get();
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
    
    std::cout << "총합: " << total << std::endl;      // 60
    std::cout << "시간: " << duration << "초" << std::endl;  // 1초
    
    return 0;
}

성능: 순차 실행 3초 → 병렬 실행 1초 (3배 개선)

5) 예외 처리

#include <future>
#include <iostream>
#include <stdexcept>
int divide(int a, int b) {
    if (b == 0) {
        throw std::runtime_error("0으로 나눌 수 없음");
    }
    return a / b;
}
int main() {
    auto future = std::async(divide, 10, 0);
    
    try {
        int result = future.get();  // 예외 재던지기
    } catch (const std::exception& e) {
        std::cout << "예외: " << e.what() << std::endl;
    }
    
    return 0;
}

6) future 상태 확인

#include <future>
#include <iostream>
#include <thread>
#include <chrono>
int longCompute() {
    std::this_thread::sleep_for(std::chrono::seconds(3));
    return 42;
}
int main() {
    auto future = std::async(std::launch::async, longCompute);
    
    // 타임아웃
    auto status = future.wait_for(std::chrono::seconds(1));
    
    if (status == std::future_status::ready) {
        std::cout << "완료" << std::endl;
    } else if (status == std::future_status::timeout) {
        std::cout << "타임아웃 (아직 실행 중)" << std::endl;
    }
    
    // 완료 대기
    future.wait();
    std::cout << "결과: " << future.get() << std::endl;
    
    return 0;
}

고급 활용

1) 병렬 다운로드

#include <future>
#include <vector>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
std::string downloadFile(const std::string& url) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return "Data from " + url;
}
int main() {
    std::vector<std::string> urls = {
        "http://example.com/file1",
        "http://example.com/file2",
        "http://example.com/file3"
    };
    
    std::vector<std::future<std::string>> futures;
    
    for (const auto& url : urls) {
        futures.push_back(std::async(std::launch::async, downloadFile, url));
    }
    
    for (auto& future : futures) {
        std::cout << future.get() << std::endl;
    }
    
    return 0;
}

2) 병렬 맵 리듀스

#include <future>
#include <vector>
#include <numeric>
#include <iostream>
int sum_range(const std::vector<int>& data, size_t start, size_t end) {
    return std::accumulate(data.begin() + start, data.begin() + end, 0);
}
int parallel_sum(const std::vector<int>& data, size_t num_threads) {
    size_t chunk_size = data.size() / num_threads;
    std::vector<std::future<int>> futures;
    
    for (size_t i = 0; i < num_threads; ++i) {
        size_t start = i * chunk_size;
        size_t end = (i == num_threads - 1) ? data.size() : (i + 1) * chunk_size;
        
        futures.push_back(std::async(std::launch::async, sum_range, 
                                     std::ref(data), start, end));
    }
    
    int total = 0;
    for (auto& future : futures) {
        total += future.get();
    }
    
    return total;
}
int main() {
    std::vector<int> data(10000000, 1);
    
    int sum = parallel_sum(data, 4);
    std::cout << "합계: " << sum << std::endl;  // 10000000
    
    return 0;
}

3) 타임아웃 패턴

#include <future>
#include <iostream>
#include <thread>
#include <chrono>
int slowCompute() {
    std::this_thread::sleep_for(std::chrono::seconds(5));
    return 42;
}
int main() {
    auto future = std::async(std::launch::async, slowCompute);
    
    auto status = future.wait_for(std::chrono::seconds(2));
    
    if (status == std::future_status::ready) {
        std::cout << "결과: " << future.get() << std::endl;
    } else {
        std::cout << "타임아웃: 기본값 사용" << std::endl;
        // future는 계속 실행 중 (취소 불가)
        // 기본값 반환 또는 다른 처리
    }
    
    return 0;
}

성능 비교

async vs thread

테스트: 간단한 계산 작업

방식코드 복잡도결과 반환예외 처리오버헤드
std::async낮음future자동중간
std::thread높음수동 (공유 변수)수동낮음

병렬 실행 벤치마크

테스트: 3개 작업, 각 1초

실행 방식시간배속
순차 실행3초1x
병렬 실행 (async)1초3x
결론: 병렬 실행으로 3배 개선

launch 정책 비교

#include <future>
#include <iostream>
#include <thread>
#include <chrono>
int compute() {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    return 42;
}
int main() {
    // launch::async
    auto start1 = std::chrono::high_resolution_clock::now();
    auto f1 = std::async(std::launch::async, compute);
    auto end1 = std::chrono::high_resolution_clock::now();
    auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end1 - start1).count();
    
    std::cout << "async 생성: " << duration1 << "us" << std::endl;
    // 약 50us (스레드 생성 비용)
    
    // launch::deferred
    auto start2 = std::chrono::high_resolution_clock::now();
    auto f2 = std::async(std::launch::deferred, compute);
    auto end2 = std::chrono::high_resolution_clock::now();
    auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start2).count();
    
    std::cout << "deferred 생성: " << duration2 << "us" << std::endl;
    // 약 1us (지연 실행)
    
    f1.get();
    f2.get();
    
    return 0;
}

실무 사례

사례 1: 웹 서버 - 병렬 요청 처리

#include <future>
#include <vector>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
struct Request {
    std::string url;
    std::string method;
};
std::string processRequest(const Request& req) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    return "Response from " + req.url;
}
int main() {
    std::vector<Request> requests = {
        {"http://api.example.com/users", "GET"},
        {"http://api.example.com/posts", "GET"},
        {"http://api.example.com/comments", "GET"}
    };
    
    std::vector<std::future<std::string>> futures;
    
    for (const auto& req : requests) {
        futures.push_back(std::async(std::launch::async, processRequest, req));
    }
    
    for (auto& future : futures) {
        std::cout << future.get() << std::endl;
    }
    
    return 0;
}

사례 2: 데이터 처리 - 병렬 파일 읽기

#include <future>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
std::string readFile(const std::string& filename) {
    std::ifstream file(filename);
    std::string content((std::istreambuf_iterator<char>(file)),
                        std::istreambuf_iterator<char>());
    return content;
}
int main() {
    std::vector<std::string> filenames = {
        "file1.txt",
        "file2.txt",
        "file3.txt"
    };
    
    std::vector<std::future<std::string>> futures;
    
    for (const auto& filename : filenames) {
        futures.push_back(std::async(std::launch::async, readFile, filename));
    }
    
    for (size_t i = 0; i < futures.size(); ++i) {
        try {
            std::string content = futures[i].get();
            std::cout << "파일 " << i << " 크기: " << content.size() << std::endl;
        } catch (const std::exception& e) {
            std::cout << "파일 " << i << " 읽기 실패: " << e.what() << std::endl;
        }
    }
    
    return 0;
}

사례 3: 게임 - 비동기 리소스 로딩

#include <future>
#include <vector>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
struct Texture {
    std::string name;
    int width, height;
};
Texture loadTexture(const std::string& filename) {
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    return {filename, 1024, 1024};
}
int main() {
    std::vector<std::string> textures = {
        "player.png",
        "enemy.png",
        "background.png"
    };
    
    std::vector<std::future<Texture>> futures;
    
    for (const auto& filename : textures) {
        futures.push_back(std::async(std::launch::async, loadTexture, filename));
    }
    
    std::cout << "로딩 중..." << std::endl;
    
    std::vector<Texture> loadedTextures;
    for (auto& future : futures) {
        loadedTextures.push_back(future.get());
    }
    
    std::cout << "로딩 완료: " << loadedTextures.size() << "개" << std::endl;
    
    return 0;
}

트러블슈팅

문제 1: future 소멸자 블로킹

증상: 프로그램이 예상치 않게 대기

// ❌ future 무시
std::async(std::launch::async, []() {
    std::this_thread::sleep_for(std::chrono::seconds(5));
});  // 소멸자에서 대기 (블로킹)
std::cout << "다음 작업" << std::endl;  // 5초 후 출력
// ✅ future 저장
auto future = std::async(std::launch::async, []() {
    std::this_thread::sleep_for(std::chrono::seconds(5));
});
std::cout << "다음 작업" << std::endl;  // 즉시 출력
future.get();  // 명시적 대기

문제 2: get 여러 번 호출

증상: std::future_error 예외

auto future = std::async([]() { return 42; });
int r1 = future.get();  // OK
// int r2 = future.get();  // 에러: future_error
// ✅ get은 한 번만
// 여러 스레드에서 공유하려면 shared_future 사용

문제 3: 데드락

증상: 프로그램이 멈춤

#include <future>
#include <mutex>
std::mutex mtx;
// ❌ 데드락 가능
void bad_pattern() {
    std::lock_guard<std::mutex> lock(mtx);
    
    auto future = std::async(std::launch::async, []() {
        std::lock_guard<std::mutex> lock(mtx);  // 데드락
        return 42;
    });
    
    future.get();
}
// ✅ 락 범위 최소화
void good_pattern() {
    auto future = std::async(std::launch::async, []() {
        std::lock_guard<std::mutex> lock(mtx);
        return 42;
    });
    
    future.get();
}

문제 4: 작은 작업의 오버헤드

증상: 병렬 실행이 오히려 느림

// ❌ 작은 작업 (스레드 생성 비용 > 작업 시간)
auto future = std::async(std::launch::async, []() {
    return 1 + 1;  // 너무 간단
});
// ✅ 충분히 큰 작업만 병렬화
auto future = std::async(std::launch::async, []() {
    // 복잡한 계산 (수백 ms 이상)
    return compute_heavy();
});

기준: 작업 시간 > 스레드 생성 비용 (약 50us)

마무리

std::async비동기 작업을 간결하게 표현하고 std::future로 결과를 받을 수 있게 합니다.

핵심 요약

  1. 기본 사용
    • std::async(func, args...)로 비동기 실행
    • future.get()으로 결과 대기
  2. launch 정책
    • launch::async: 즉시 실행 (새 스레드)
    • launch::deferred: 지연 실행 (get 호출 시)
    • 기본: async | deferred (구현 의존)
  3. 예외 처리
    • 예외는 get() 호출 시 재던지기
    • try-catch로 처리
  4. 주의사항
    • future 소멸자는 블로킹
    • get()은 한 번만 호출 가능
    • 작은 작업은 오버헤드 주의

선택 가이드

상황방법
간단한 비동기 작업std::async
결과 반환 필요std::async + future.get()
여러 스레드에서 결과 공유shared_future
세밀한 스레드 제어std::thread

코드 예제 치트시트

// 기본 사용
auto future = std::async(func, args...);
int result = future.get();
// launch::async (즉시 실행)
auto f1 = std::async(std::launch::async, func);
// launch::deferred (지연 실행)
auto f2 = std::async(std::launch::deferred, func);
// 타임아웃
auto status = future.wait_for(std::chrono::seconds(1));
if (status == std::future_status::ready) {
    // 완료
}
// 예외 처리
try {
    future.get();
} catch (const std::exception& e) {
    // 처리
}

다음 단계

참고 자료

  • “C++ Concurrency in Action” - Anthony Williams
  • “Effective Modern C++” - Scott Meyers
  • cppreference: https://en.cppreference.com/w/cpp/thread/async 한 줄 정리: std::async는 비동기 작업을 간결하게 표현하며, launch 정책으로 실행 시점을 제어하고 future로 결과를 안전하게 받는다.

심화 부록: 구현·운영 관점

이 부록은 앞선 본문에서 다룬 주제(「C++ async & launch | std::async·future·launch 정책 완벽 정리」)를 구현·런타임·운영 관점에서 다시 압축합니다. 도메인별 세부 구현은 글마다 다르지만, 입력 검증 → 핵심 연산 → 부작용(I/O·네트워크·동시성) → 관측의 흐름으로 장애를 나누면 원인 추적이 빨라집니다.

내부 동작과 핵심 메커니즘

flowchart TD
  A[입력·요청·이벤트] --> B[파싱·검증·디코딩]
  B --> C[핵심 연산·상태 전이]
  C --> D[부작용: I/O·네트워크·동시성]
  D --> E[결과·관측·저장]
sequenceDiagram
  participant C as 클라이언트/호출자
  participant B as 경계(런타임·게이트웨이·프로세스)
  participant D as 의존성(API·DB·큐·파일)
  C->>B: 요청/이벤트
  B->>D: 조회·쓰기·RPC
  D-->>B: 지연·부분 실패·재시도 가능
  B-->>C: 응답 또는 오류(코드·상관 ID)
  • 불변 조건(Invariant): 버퍼 경계, 프로토콜 상태, 트랜잭션 격리, FD 상한 등 단계별로 문장으로 적어 두면 디버깅 비용이 줄어듭니다.
  • 결정성: 순수 층과 시간·네트워크·스케줄에 의존하는 층을 분리해야 테스트와 장애 분석이 쉬워집니다.
  • 경계 비용: 직렬화, 인코딩, syscall 횟수, 락 경합, 할당·GC, 캐시 미스를 의심 목록에 둡니다.
  • 백프레셔: 생산자가 소비자보다 빠를 때 버퍼·큐·스트림에서 속도를 줄이는 신호를 어디에 둘지 정의합니다.

프로덕션 운영 패턴

영역운영 관점 질문
관측성요청 단위 상관 ID, 에러율·지연 p95/p99, 의존성 타임아웃·재시도가 대시보드에 보이는가
안전성입력 검증·권한·비밀·감사 로그가 코드 경로마다 일관적인가
신뢰성재시도는 멱등 연산에만 적용되는가, 서킷 브레이커·백오프·DLQ가 있는가
성능캐시·배치 크기·커넥션 풀·인덱스·백프레셔가 데이터 규모에 맞는가
배포롤백 룬북, 카나리/블루그린, 마이그레이션·피처 플래그가 문서화되어 있는가
용량피크 트래픽·디스크·FD·스레드 풀 상한을 주기적으로 검증하는가

스테이징은 데이터 양·네트워크 RTT·동시성을 프로덕션에 가깝게 맞출수록 재현율이 올라갑니다.

확장 예시: 엔드투엔드 미니 시나리오

앞선 본문 주제(「C++ async & launch | std::async·future·launch 정책 완벽 정리」)를 배포·운영 흐름에 맞춰 옮긴 체크리스트입니다. 도메인에 맞게 단계 이름만 바꿔 적용할 수 있습니다.

  1. 입력 계약 고정: 스키마·버전·최대 페이로드·타임아웃·에러 코드를 경계에 둔다.
  2. 핵심 경로 계측: 요청 ID, 단계별 지연, 외부 호출 결과 코드를 로그·메트릭·트레이스에서 한 흐름으로 본다.
  3. 실패 주입: 의존성 타임아웃·5xx·부분 데이터·락 대기를 스테이징에서 재현한다.
  4. 호환·롤백: 설정/마이그레이션/클라이언트 버전을 되돌릴 수 있는지 확인한다.
  5. 부하 후 검증: 피크 대비 p95/p99, 에러율, 리소스 상한, 알림 임계값을 점검한다.
handle(request):
  ctx = newCorrelationId()
  validated = validateSchema(request)
  authorize(validated, ctx)
  result = domainCore(validated)
  persistOrEmit(result, idempotentKey)
  recordMetrics(ctx, latency, outcome)
  return result

문제 해결(Troubleshooting)

증상가능 원인조치
간헐적 실패레이스, 타임아웃, 외부 의존성, DNS최소 재현 스크립트, 분산 트레이스·로그 상관관계, 재시도·서킷 설정 점검
성능 저하N+1, 동기 I/O, 락 경합, 과도한 직렬화, 캐시 미스프로파일러·APM으로 핫스팟 확인 후 한 가지씩 제거
메모리 증가캐시 무제한, 구독/리스너 누수, 대용량 버퍼, 커넥션 미반납상한·TTL·힙/FD 스냅샷 비교
빌드·배포만 실패환경 변수, 권한, 플랫폼 차이, lockfileCI 로그와 로컬 diff, 런타임·이미지 버전 핀
설정 불일치프로필·시크릿·기본값, 리전스키마 검증된 설정 단일 소스와 배포 매트릭스 표준화
데이터 불일치비멱등 재시도, 부분 쓰기, 캐시 무효화 누락멱등 키·아웃박스·트랜잭션 경계 재검토

권장 순서: (1) 최소 재현 (2) 최근 변경 범위 축소 (3) 환경·의존성 차이 (4) 관측으로 가설 검증 (5) 수정 후 회귀·부하 테스트.

배포 전에는 git addgit commitgit pushnpm run deploy 순서를 권장합니다.


자주 묻는 질문 (FAQ)

Q. 이 내용을 실무에서 언제 쓰나요?

A. std::async는 함수를 비동기로 실행하고 future로 결과를 받는 C++11 API입니다. launch::async, launch::deferred 정책과 실전 예제를 정리합니다. Start now. 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.

Q. 선행으로 읽으면 좋은 글은?

A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.

Q. 더 깊이 공부하려면?

A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.


같이 보면 좋은 글 (내부 링크)

이 주제와 연결되는 다른 글입니다.


이 글에서 다루는 키워드 (관련 검색어)

C++, async, launch, future, C++11, 비동기 등으로 검색하시면 이 글이 도움이 됩니다.