std::promise<R>:: set_exception
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Getting the result | ||||
| Setting the result | ||||
|
promise::set_exception
|
||||
| Non-member Functions | ||||
| Helper Classes | ||||
|
void
set_exception
(
std::
exception_ptr
p
)
;
|
(C++11 이후) | |
공유 상태에 예외 포인터 p 를 원자적으로 저장하고 상태를 준비 완료로 만듭니다.
이 연산은 promise 객체를 업데이트하는 동안
set_value
,
set_exception
,
set_value_at_thread_exit
, 그리고
set_exception_at_thread_exit
이 promise 객체와 연관된 단일 뮤텍스를 획득하는 것처럼 동작합니다.
공유 상태가 없거나 공유 상태가 이미 값이나 예외를 저장하고 있는 경우 예외가 발생합니다.
이 함수에 대한 호출은 get_future 호출과 데이터 경쟁을 발생시키지 않습니다 (따라서 서로 동기화할 필요가 없습니다).
목차 |
매개변수
| p | - | 저장할 예외 포인터. p 가 null인 경우 동작은 정의되지 않음 |
반환값
(없음)
예외
std::future_error 가 다음 조건에서 발생합니다:
- * this 는 공유 상태를 가지지 않습니다. 오류 코드는 no_state 로 설정됩니다.
- 공유 상태가 이미 값이나 예외를 저장하고 있습니다. 오류 코드는 promise_already_satisfied 로 설정됩니다.
예제
#include <future> #include <iostream> #include <thread> int main() { std::promise<int> p; std::future<int> f = p.get_future(); std::thread t([&p] { try { // 예외를 발생시킬 수 있는 코드 throw std::runtime_error("Example"); } catch (...) { try { // promise에 발생한 모든 예외 저장 p.set_exception(std::current_exception()); // 또는 사용자 정의 예외를 대신 발생시킬 수 있음 // p.set_exception(std::make_exception_ptr(MyException("mine"))); } catch (...) {} // set_exception()도 예외를 발생시킬 수 있음 } }); try { std::cout << f.get(); } catch (const std::exception& e) { std::cout << "스레드에서 발생한 예외: " << e.what() << '\n'; } t.join(); }
출력:
Exception from the thread: Example
참고 항목
|
스레드 종료 시에만 알림을 전달하면서 결과를 예외로 설정함
(public member function) |