以下のコードで、std::packaged_taskが呼ぶfuncからthrowした例外と、異なる例外がcatchされます。
std::asyncが呼ぶfuncの場合は、throwした例外と同じ例外がcatchされます。
なぜ、差異があるのでしょうか。
C++
1#include <iostream> 2#include <future> 3 4void func() { 5 throw 1; 6} 7 8int main() { 9 std::packaged_task<void()> task(func); 10 std::thread th(std::move(task)); 11 th.join(); 12 try { 13 task.get_future().get(); 14 } catch(const int& err) { 15 std::cout << err << std::endl; 16 } catch(...) { 17 // catchされる 18 std::cout << "..." << std::endl; 19 } 20 21 auto fut = std::async(std::launch::async, func); 22 try { 23 fut.get(); 24 } catch(const int& err) { 25 // catchされる 26 std::cout << err << std::endl; 27 } catch (...) { 28 std::cout << "..." << std::endl; 29 } 30}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/10 09:48 編集
2016/04/10 10:00