![](/img/trans.png)
[英]C++11 std::thread join crashes with system_error exception and SIGABRT on Xcode 6?
[英]SIGABRT signal received when creating a std::thread c++11
我在類成員方法中創建一個線程,如下所示:
void MyClass::startThread()
{
T.reset( new std::thread( &MyClass::myThreadMethod, this ) );
}
void MyClass::myThreadMethod()
{
// ...
}
哪里
// In header file
std::unique_ptr<std::thread> T;
當我運行MyClass::startThread()
,我會收到:
收到的信號:SIGABRT(已中止)......
如果我執行代碼,它會在線程構造函數中發生。
我試圖像這樣刪除unique_ptr
:
void MyClass::startThread()
{
std::thread* T = new std::thread( &MyClass::myThreadMethod, this );
}
並發生了同樣的事情。 我在Linux / Kubuntu 12.04上的NetBeans 7.4上使用gcc 4.8.2。
有人知道會發生什么嗎?
當沒有事先調用std::thread::detach()
或std::thread::join()
而銷毀std::thread
時會發生這種情況。 你應該調用兩者中的任何一個,調用什么取決於你想要的行為。
void MyClass::startThread() {
T.reset( new std::thread( &MyClass::myThreadMethod, this ) );
T->join(); // Wait for thread to finish
}
要么
void MyClass::startThread() {
T.reset( new std::thread( &MyClass::myThreadMethod, this ) );
T->detach(); // Leave thread on its own (do not wait for it to finish)
}
作為旁注,您可以通過使std::thread
本身成為成員來刪除對std::unique_ptr
的使用:
class MyClass {
std::thread t;
};
要分配t
一個線程,你可以構建一個與移動它分配給t
:
t = std::thread(&MyClass::myThreadMethod, this);
根據Mark Garcia的建議和示例,根據這個問題,我剛剛添加了-pthread
作為編譯器的選項。
由於未知原因,我的其他項目正常工作,但我認為這是由於Boost或Open CV必須包含當前測試中缺少的內容。
無論如何,目前,它的工作原理。
謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.