
parallelFuture::value는 Future의 결과를 즉시 반환하는 함수입니다.
예를 들어, Future가 완료된 후의 결과를 즉시 사용해야 하는 경우에 사용할 수 있습니다.
다음은 간단한 예제입니다.
#hostingforum.kr
cpp
#include
#include
int main() {
std::future future = std::async(std::launch::async, [](){ return 10; });
int result = future.get(); // Future가 완료된 후의 결과를 반환합니다.
std::cout << result << std::endl; // 10을 출력합니다.
return 0;
}
parallelFuture::value를 사용한 장점은 Future의 결과를 즉시 반환할 수 있기 때문에 Future가 완료된 후의 결과를 사용할 수 있습니다.
다만, Future가 완료되지 않은 경우 std::future_status::ready가 반환되지 않기 때문에 Future가 완료된 후의 결과를 사용할 수 있습니다.
또한, Future가 완료되지 않은 경우 std::future_status::timeout이 반환될 수 있으므로, Future가 완료되지 않은 경우 예외 처리를 해야 합니다.
#hostingforum.kr
cpp
#include
#include
int main() {
std::future future = std::async(std::launch::async, [](){ return 10; });
try {
int result = future.value(); // Future가 완료된 후의 결과를 반환합니다.
std::cout << result << std::endl; // 10을 출력합니다.
} catch (const std::future_error& e) {
if (e.code() == std::future_errc::no_state) {
std::cout << "Future가 완료되지 않았습니다." << std::endl;
} else if (e.code() == std::future_errc::timeout) {
std::cout << "Future가 완료되지 않았습니다." << std::endl;
}
}
return 0;
}
2025-07-23 02:11