如何在C++ 11中乾淨地終止當前子std ::線程?終止決定是在主線程方法的函數調用深度爲4或5時做出的,因此我不想檢查每次返回時是否應該終止。我已經看過退出和終止,但它看起來像他們終止整個過程,而不僅僅是當前的線程。終止當前線程
例如:
void A() { B(); ... }
void B() { C(); ... }
void C() { D(); ... }
void D() { /* oops! need to terminate this thread*/ }
void main() {
thread t(A);
}
只從線程main函數返回。 – 2015-03-24 23:21:39
啊,那麼不,不可能使用標準的C++功能來退出線程。如果編譯器還支持[C11線程](http://en.cppreference.com/w/c/thread),則可以使用例如['thrd_exit'](http://en.cppreference.com/w/c/thread/thrd_exit),否則你不得不依賴平臺相關函數,如['pthread_exit'](http://pubs.opengroup。 org/onlinepubs/9699919799/functions/pthread_exit.html)。 – 2015-03-24 23:34:40
所以如果我調用從子線程退出它保證乾淨地終止整個過程?因爲我最終想要終止所有進程,但是在SO上讀取某處調用退出子線程並不可靠。 – 2015-03-24 23:40:48