4

스레드 내에서 예외를 발생시키고 호출 프로세스가이를 catch 할 수 있습니다. 그러나 이로 인해 전체 응용 프로그램이 중단되는 것으로 보입니다. 테스트 코드 첨부가 절대로 exit 문을 출력하지 않습니다.부스트 스레드 예외 처리

1 #include <boost/thread.hpp> 
2 #include <iostream> 
3 
4 void wait(int seconds) 
5 { 
6  boost::this_thread::sleep(boost::posix_time::seconds(seconds)); 
7 } 
8 
9 void thread() 
10 { 
11  for (int i = 0; i < 5; ++i) 
12  { 
13   wait(1); 
14   std::cout << i << std::endl; 
15  } 
16  throw; 
17 } 
18 
19 int main() 
20 { 
21  try 
22  { 
23   boost::thread t(thread); 
24   t.join(); 
25   std::cout << "Exit normally\n"; 
26  } 
27  catch (...) 
28  { 
29   std::cout << "Caught Exception\n"; 
30  } 
31 } 

답변

5

부스트 예외를 살펴보십시오. Transporting of Exceptions Between Threads. 이 접근 방식은 저에게 효과적입니다.

+2

이것은 C++ 11에서 더욱 쉬워졌다. (어떤 예외도'std :: exception_ptr'에서 캡쳐 될 수있다.) – Cubbi

+0

@Cubbi 정보를 제공해 주셔서 감사합니다. 나는 그것을 깨닫지 못했습니다. :) – Ralf