[問題] C++ Exception interactions with Threads

看板C_and_CPP (C/C++)作者 (小乖)時間15年前 (2010/12/16 22:32), 編輯推噓2(207)
留言9則, 4人參與, 最新討論串1/1
抱歉 上面那一篇文章我沒有查清楚原因就發問 問題是這樣,下面的 test() 無法捕捉到 hello 丟出的例外 我用 debug 模式下 trace 了一下堆疊的狀態 hello 和 main 運行在不同的 threads (也就是不同的堆疊) 我想問的是如果 test 必須處理 hello 所丟出的例外 有甚麼方式能夠做到呢? 謝謝 #include <iostream> #include <boost/thread/thread.hpp> void hello() { std::cout << "Hello world, I''m a thread!" << std::endl; throw "exception"; } void test() { try{ boost::thread thrd(&hello); thrd.join(); }catch(...){ std::cout << "exception"; } } int main() { test(); } ps http://www.gamedev.net/reference/articles/article953.asp 網路上的文章,有提到 exception 跟 thread XD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.134.96.34

12/16 22:36, , 1F
跑成員函式, 先接再用attribute記錄錯誤類型, 回主執
12/16 22:36, 1F

12/16 22:37, , 2F
行緒再丟一次
12/16 22:37, 2F

12/17 09:32, , 3F
boost exception有解法 http://tinyurl.com/2dwbts8
12/17 09:32, 3F

12/17 14:16, , 4F
請問一下 "先接的意思是?成員函式拋出的例外能被捕捉嗎
12/17 14:16, 4F

12/17 14:19, , 5F
跨執行緒把例外往外丟前要先存在某處, 這邊你可以寫一
12/17 14:19, 5F

12/17 14:20, , 6F
個類別專門儲存例外物件, 而執行緒執行它某一個非靜態
12/17 14:20, 6F

12/17 14:22, , 7F
成員函式, 將例外儲存起來, 執行緒結束後在主執行緒裡
12/17 14:22, 7F

12/17 14:23, , 8F
再將例外丟出一次, 然後看你怎麼處理
12/17 14:23, 8F

12/17 19:30, , 9F
我是在 thread 裡吞掉之後再用 signal/slot 傳給 main
12/17 19:30, 9F
文章代碼(AID): #1D2YBeiy (C_and_CPP)
文章代碼(AID): #1D2YBeiy (C_and_CPP)