C++ shared_future | 여러 스레드에서 future 결과 공유

C++ shared_future | 여러 스레드에서 future 결과 공유

이 글의 핵심

C++ shared_future에 대한 실전 가이드입니다. 여러 스레드에서 future 결과 공유 등을 예제와 함께 상세히 설명합니다.

shared_future란?

std::future는 결과를 한 번만 get() 할 수 있고, 이동만 가능합니다. std::shared_future는 복사 가능하며, 여러 스레드가 각자 복사본을 가지고 동일한 비동기 결과를 get() 할 수 있습니다. promise와 future, async로 만든 결과를 여러 스레드가 나눠 쓸 때 유용하며, 스레드 기초를 알고 있으면 이해하기 쉽습니다.

future와의 차이

특성std::futurestd::shared_future
복사불가 (이동만 가능)가능
get() 횟수한 번만 (이후 invalid)복사본마다 한 번씩
스레드 공유불가가능 (복사본 전달)
사용 시나리오단일 스레드 결과 수신여러 스레드 결과 공유
// future: 이동만 가능
std::future<int> f1 = std::async([]{ return 42; });
// std::future<int> f2 = f1;  // 에러! 복사 불가
std::future<int> f2 = std::move(f1);  // OK: 이동
int result = f2.get();  // OK
// int result2 = f2.get();  // 에러! 두 번째 get() 불가

// shared_future: 복사 가능
std::shared_future<int> sf1 = std::async([]{ return 42; }).share();
std::shared_future<int> sf2 = sf1;  // OK: 복사
int r1 = sf1.get();  // OK
int r2 = sf2.get();  // OK: 다른 복사본에서 get()

기본 사용

#include <future>
#include <iostream>
#include <thread>

int main() {
    std::promise<int> p;
    std::shared_future<int> f = p.get_future().share();

    std::thread t1([f]() { std::cout << "t1: " << f.get() << '\n'; });
    std::thread t2([f]() { std::cout << "t2: " << f.get() << '\n'; });

    p.set_value(42);
    t1.join();
    t2.join();
    return 0;
}

동작 원리: share()를 호출하면 기존 future는 invalid 상태가 되고, 반환된 shared_future를 복사해 각 스레드에 전달합니다. 람다에서 값으로 캡처 [f] 하면 스레드마다 복사본이 생기므로 각자 get() 한 번씩 호출할 수 있습니다.

실무 팁:

  • 값 캡처 사용: [f]로 값 캡처하면 각 스레드가 독립적인 복사본을 가짐
  • 참조 캡처 주의: [&f]로 참조 캡처하면 같은 객체를 공유하므로, 한 스레드에서 get() 호출 후 다른 스레드에서 호출 시 UB
// ✅ 올바른 사용: 값 캡처
std::shared_future<int> sf = ...;
std::thread t1([sf]() { int r = sf.get(); });  // 복사본
std::thread t2([sf]() { int r = sf.get(); });  // 복사본

// ❌ 잘못된 사용: 참조 캡처
std::thread t3([&sf]() { int r = sf.get(); });  // 같은 객체
std::thread t4([&sf]() { int r = sf.get(); });  // UB!

예외 전파

promise에서 set_exception으로 예외를 넣으면, shared_future를 get()하는 모든 스레드에서 동일한 예외가 전파됩니다. 여러 스레드가 같은 실패 결과를 처리할 때 일관성이 있습니다.

#include <future>
#include <iostream>
#include <thread>
#include <exception>

std::promise<int> p;
auto sf = p.get_future().share();

std::thread t1([sf]() {
    try { 
        int result = sf.get(); 
        std::cout << "t1: " << result << '\n';
    }
    catch (const std::exception& e) { 
        std::cout << "t1 caught: " << e.what() << '\n'; 
    }
});

std::thread t2([sf]() {
    try { 
        int result = sf.get(); 
        std::cout << "t2: " << result << '\n';
    }
    catch (const std::exception& e) { 
        std::cout << "t2 caught: " << e.what() << '\n'; 
    }
});

// 예외 설정
p.set_exception(std::make_exception_ptr(std::runtime_error("failed")));

t1.join();  // "t1 caught: failed"
t2.join();  // "t2 caught: failed"

동작 원리: shared_future는 내부적으로 공유 상태(shared state) 를 참조 카운팅으로 관리합니다. 예외가 설정되면, 모든 복사본에서 get() 호출 시 동일한 예외가 다시 던져집니다.

실무 활용: 데이터베이스 연결 실패 시, 모든 워커 스레드가 동일한 예외를 받아 일관되게 처리할 수 있습니다.

std::promise<DatabaseConnection> conn_promise;
auto conn_future = conn_promise.get_future().share();

std::thread connector([&]() {
    try {
        auto conn = connect_to_database();
        conn_promise.set_value(std::move(conn));
    } catch (...) {
        conn_promise.set_exception(std::current_exception());
    }
});

// 여러 워커가 연결 대기
std::vector<std::thread> workers;
for (int i = 0; i < 4; ++i) {
    workers.emplace_back([conn_future]() {
        try {
            auto conn = conn_future.get();
            // ... DB 작업 ...
        } catch (const std::exception& e) {
            std::cerr << "Worker failed: " << e.what() << '\n';
        }
    });
}

connector.join();
for (auto& w : workers) w.join();

스레드 안전성

shared_future는 스레드 안전한가?:

  • 복사본 간: 안전 (각 복사본은 독립적)
  • 같은 객체: 불안전 (동시 get() 호출 시 UB)
std::shared_future<int> sf = ...;

// ✅ 안전: 각 스레드가 복사본 사용
std::thread t1([sf]() { int r = sf.get(); });  // sf의 복사본
std::thread t2([sf]() { int r = sf.get(); });  // sf의 복사본

// ❌ 불안전: 같은 객체를 참조로 공유
std::thread t3([&sf]() { int r = sf.get(); });  // 같은 sf
std::thread t4([&sf]() { int r = sf.get(); });  // UB!

내부 동작: shared_future는 내부적으로 공유 상태(shared state) 를 가리키는 포인터를 가지고 있습니다. 복사하면 포인터가 복사되고 참조 카운트가 증가합니다. get() 호출 시 공유 상태에서 결과를 읽어오는데, 이 읽기 자체는 스레드 안전하지만, 같은 shared_future 객체에서 동시에 get()을 호출하면 객체 내부 상태 변경으로 인해 UB가 발생합니다.

권장 패턴:

// ✅ 패턴 1: 값 캡처로 복사본 전달
std::shared_future<int> sf = ...;
std::thread t1([sf]() { /* ... */ });
std::thread t2([sf]() { /* ... */ });

// ✅ 패턴 2: 명시적 복사 후 참조 전달
std::shared_future<int> sf1 = sf;
std::shared_future<int> sf2 = sf;
std::thread t1([&sf1]() { /* ... */ });
std::thread t2([&sf2]() { /* ... */ });

// ❌ 패턴 3: 참조 캡처 (UB)
std::thread t1([&sf]() { /* ... */ });
std::thread t2([&sf]() { /* ... */ });

실전 예시: 여러 워커가 한 번의 설정값 사용

설정을 비동기로 로드한 뒤, 여러 스레드가 그 결과를 읽는 패턴입니다.

#include <future>
#include <thread>
#include <vector>
#include <iostream>

struct Config {
    std::string db_url;
    int max_connections;
};

Config load_config_from_disk() {
    // ... 파일에서 설정 로드 ...
    return {"localhost:5432", 10};
}

void do_work(const Config& cfg) {
    std::cout << "Working with DB: " << cfg.db_url << '\n';
}

int main() {
    std::promise<Config> config_promise;
    std::shared_future<Config> config_future = config_promise.get_future().share();

    // 설정 로더 스레드
    std::thread loader([&config_promise]() {
        Config c = load_config_from_disk();
        config_promise.set_value(std::move(c));
    });

    // 워커 스레드들 (각자 설정 복사본 사용)
    std::vector<std::thread> workers;
    for (int i = 0; i < 4; ++i) {
        workers.emplace_back([config_future, i]() {  // 값 캡처
            const Config& cfg = config_future.get();  // 각 스레드에서 한 번씩
            std::cout << "Worker " << i << " got config\n";
            do_work(cfg);
        });
    }

    loader.join();
    for (auto& w : workers) w.join();
    return 0;
}

왜 shared_future를 사용하나?: 설정 파일을 한 번만 로드하고, 여러 워커가 동일한 설정을 사용합니다. future를 사용하면 한 워커만 결과를 받을 수 있지만, shared_future를 사용하면 모든 워커가 결과를 받을 수 있습니다.

실전 예시: shared_future로 캐시/지연 계산 공유

한 번만 계산하고, 여러 호출자가 결과를 공유하는 패턴에도 쓸 수 있습니다. (실제 캐시는 락이나 atomic 등으로 보강하는 경우가 많지만, shared_future가 “한 번만 채워지는 값” 역할을 할 수 있음.)

std::promise<HeavyResult> p;
std::shared_future<HeavyResult> sf = p.get_future().share();

std::thread producer([&p]() { p.set_value(compute_heavy()); });
std::vector<std::thread> consumers;
for (int i = 0; i < 3; ++i) {
    consumers.emplace_back([sf]() { use(sf.get()); });
}
producer.join();
for (auto& c : consumers) c.join();

자주 발생하는 문제

1. get() 중복 호출

std::shared_future<int> sf = ...;

// ❌ 같은 복사본에서 get() 두 번 호출
int r1 = sf.get();  // OK
int r2 = sf.get();  // UB!

// ✅ 결과를 변수에 저장해 재사용
int result = sf.get();
use(result);
use(result);  // OK

왜 문제인가?: shared_future::get()은 내부적으로 상태를 변경합니다. 같은 객체에서 두 번 호출하면 정의되지 않은 동작입니다.

실무 해결책:

// 패턴 1: 결과를 지역 변수에 저장
void worker(std::shared_future<Config> sf) {
    const Config& cfg = sf.get();  // 한 번만 호출
    for (int i = 0; i < 10; ++i) {
        use_config(cfg);  // 저장된 결과 재사용
    }
}

// 패턴 2: 여러 스레드가 각자 복사본 사용
std::shared_future<int> sf = ...;
std::thread t1([sf]() { int r = sf.get(); });  // 복사본 1
std::thread t2([sf]() { int r = sf.get(); });  // 복사본 2

2. promise를 너무 늦게 set_value/set_exception

// ❌ 예외 발생 시 set을 안 하면 데드락
void bad_producer(std::promise<int>& p) {
    try {
        int result = compute();
        p.set_value(result);
    } catch (...) {
        // set_exception을 안 함!
    }
    // promise 소멸 시 broken_promise 예외 발생하지만, 이미 늦음
}

// ✅ 모든 경로에서 set 호출
void good_producer(std::promise<int>& p) {
    try {
        int result = compute();
        p.set_value(result);
    } catch (...) {
        p.set_exception(std::current_exception());
    }
}

실무 패턴 - RAII로 보장:

// promise가 항상 set되도록 보장
template<typename T>
class PromiseGuard {
    std::promise<T>& promise_;
    bool set_ = false;
public:
    explicit PromiseGuard(std::promise<T>& p) : promise_(p) {}
    
    ~PromiseGuard() {
        if (!set_) {
            promise_.set_exception(
                std::make_exception_ptr(std::runtime_error("Promise not set"))
            );
        }
    }
    
    void set_value(T value) {
        promise_.set_value(std::move(value));
        set_ = true;
    }
    
    void set_exception(std::exception_ptr e) {
        promise_.set_exception(e);
        set_ = true;
    }
};

void safe_producer(std::promise<int>& p) {
    PromiseGuard guard(p);
    int result = compute();
    guard.set_value(result);  // 예외 발생해도 소멸자가 set_exception 호출
}

3. future 대신 shared_future 필요 여부

// ❌ 불필요하게 shared_future 사용
std::shared_future<int> sf = std::async([]{ return 42; }).share();
int result = sf.get();  // 한 스레드만 사용하면 future로 충분

// ✅ future로 충분
std::future<int> f = std::async([]{ return 42; });
int result = f.get();

// ✅ 여러 스레드가 사용할 때만 shared_future
std::shared_future<int> sf = std::async([]{ return 42; }).share();
std::thread t1([sf]() { use(sf.get()); });
std::thread t2([sf]() { use(sf.get()); });

성능 고려사항: shared_future는 참조 카운팅 오버헤드가 있으므로, 단일 스레드에서만 사용한다면 future가 더 효율적입니다.

4. 참조 캡처로 인한 UB

std::shared_future<int> sf = ...;

// ❌ 참조 캡처: 같은 객체를 여러 스레드가 공유
std::thread t1([&sf]() { int r = sf.get(); });
std::thread t2([&sf]() { int r = sf.get(); });  // UB!

// ✅ 값 캡처: 각 스레드가 복사본 사용
std::thread t1([sf]() { int r = sf.get(); });
std::thread t2([sf]() { int r = sf.get(); });  // OK

실무 패턴

패턴 1: 브로드캐스트 알림

// 여러 스레드가 특정 이벤트를 대기
class EventBroadcaster {
    std::promise<void> event_promise_;
    std::shared_future<void> event_future_;
    
public:
    EventBroadcaster() 
        : event_future_(event_promise_.get_future().share()) {}
    
    void trigger() {
        event_promise_.set_value();  // 모든 대기 스레드 깨움
    }
    
    std::shared_future<void> subscribe() {
        return event_future_;  // 복사본 반환
    }
};

// 사용
EventBroadcaster broadcaster;
std::thread t1([f = broadcaster.subscribe()]() { f.wait(); do_work(); });
std::thread t2([f = broadcaster.subscribe()]() { f.wait(); do_work(); });
broadcaster.trigger();  // 모든 스레드 동시 시작

패턴 2: 여러 단계 파이프라인

// 단계별로 결과를 공유하는 파이프라인
std::promise<Data> stage1_promise;
auto stage1_future = stage1_promise.get_future().share();

std::promise<ProcessedData> stage2_promise;
auto stage2_future = stage2_promise.get_future().share();

// Stage 1: 데이터 로드
std::thread loader([&]() {
    stage1_promise.set_value(load_data());
});

// Stage 2: 여러 프로세서가 Stage 1 결과 대기
std::vector<std::thread> processors;
for (int i = 0; i < 3; ++i) {
    processors.emplace_back([stage1_future, i]() {
        auto data = stage1_future.get();
        process(data, i);
    });
}

// Stage 3: 최종 결과 대기
std::thread finalizer([stage2_future]() {
    auto result = stage2_future.get();
    finalize(result);
});

패턴 3: 타임아웃과 함께 사용

// shared_future와 wait_for로 타임아웃 처리
std::shared_future<int> sf = ...;

std::thread worker([sf]() {
    using namespace std::chrono;
    
    if (sf.wait_for(5s) == std::future_status::ready) {
        int result = sf.get();
        std::cout << "Got result: " << result << '\n';
    } else {
        std::cout << "Timeout!\n";
    }
});

정리

항목설명
용도하나의 비동기 결과를 여러 스레드가 공유해 읽을 때
생성future.share()로 얻고, 복사해 각 스레드에 전달
스레드 안전성복사본 간은 안전, 같은 객체는 불안전
주의get()은 각 복사본당 한 번만 호출

FAQ

Q: shared_future와 future의 가장 큰 차이는?

A: future는 이동만 가능하고 get()을 한 번만 호출할 수 있지만, shared_future는 복사 가능하고 각 복사본에서 get()을 한 번씩 호출할 수 있습니다. 여러 스레드가 같은 결과를 읽을 때 shared_future를 사용합니다.

Q: shared_future는 스레드 안전한가요?

A: 복사본 간에는 안전합니다. 각 스레드가 값 캡처로 복사본을 가지면 안전하게 get()을 호출할 수 있습니다. 하지만 같은 shared_future 객체를 여러 스레드가 참조로 공유하면 UB입니다.

Q: get()을 여러 번 호출하려면?

A: 같은 복사본에서는 get()을 한 번만 호출하세요. 결과를 지역 변수에 저장해 재사용하거나, 여러 스레드가 각자 복사본을 가지고 각각 get()을 호출하세요.

Q: promise를 set하지 않으면?

A: promise가 소멸될 때 std::future_error (broken_promise)가 발생하고, get() 대기 중인 모든 스레드에서 예외가 던져집니다. 모든 경로에서 set_value() 또는 set_exception()을 호출하세요.

Q: 언제 future 대신 shared_future를 사용하나요?

A: 여러 스레드가 같은 비동기 결과를 읽어야 할 때만 shared_future를 사용하세요. 단일 스레드만 결과를 받으면 future로 충분합니다.

관련 글: future와 promise, async 비동기 실행, 스레드 기초.

한 줄 요약: shared_future로 promise/future 결과를 여러 스레드가 안전하게 공유해 읽을 수 있습니다.


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

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

  • C++ future와 promise | “비동기” 가이드
  • C++ async & launch | “비동기 실행” 가이드
  • C++ std::thread 입문 | join 누락·디태치 남용 등 자주 하는 실수 3가지와 해결법

관련 글

  • C++ async & launch |
  • C++ packaged_task |
  • C++ Atomic Operations |
  • C++ future와 promise |
  • C++ Memory Order |