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

作者: Wush978 (拒看低質媒體)   2018-07-26 13:47:53
[關鍵字]: 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
有大大解釋得很清楚
作者: Wush978 (拒看低質媒體)   2018-07-26 22:36:00
結果我自己寫了以後,雖然可以中斷,但是還是會有memory leak 的狀況... 總之儘量避免使用吧

Links booklink

Contact Us: admin [ a t ] ucptt.com