[分享] 在 C/C++ 中回應使用者的中斷

看板R_Language作者 (拒看低質媒體)時間6年前 (2018/07/26 13:48), 6年前編輯推噓0(002)
留言2則, 1人參與, 6年前最新討論串1/1
[關鍵字]: R, C, C++, 中斷 最近在爬code的時候看到有些套件會在C loop 中插入一段檢查 user interept 的程式碼 以 dbscan 套件為例子,裡面有: for (int i = 0;i < nrow; i++) { if (!(i % 100)) Rcpp::checkUserInterrupt(); // ... } 查了一下 Rcpp 的文件, http://dirk.eddelbuettel.com/code/rcpp/Rcpp-attributes.pdf 裡面提到這個函數會適當的呼叫C++物件的destructors,所以可以在C++中安全使用。 ps. 它會丟出一個exception: Rcpp::internal::InterruptedException R原生的C API有 R_CheckUserInterrupt,但是如果使用它,則會直接跳回R C++的destructors不會被呼叫,會導致memory leak 底層的細節可以參考: http://r.789695.n4.nabble.com/Interrupting-C-code-execution-td3472972.html 有大大解釋得很清楚 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.169.113.197 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1532584080.A.946.html

07/26 22:36, 6年前 , 1F
結果我自己寫了以後,雖然可以中斷,但是還是會有
07/26 22:36, 1F

07/26 22:36, 6年前 , 2F
memory leak 的狀況... 總之儘量避免使用吧
07/26 22:36, 2F
我把細節丟到 Rcpp-devel 了: http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2018-July/010057.html 這裡也做個紀錄 ※ 編輯: Wush978 (118.169.113.197), 07/26/2018 23:34:25
文章代碼(AID): #1RMM2Gb6 (R_Language)
文章代碼(AID): #1RMM2Gb6 (R_Language)