2016-07-26 74 views
-1

我想創建一個線程並保持對它的引用作爲類的標記,並且線程調用該類的一個方法。這裏是代碼:創建一個線程作爲一個類的數據成員

EventQueue::EventQueue() { 

    this->dispatcherThread = std::thread(&EventQueue::dispatchEvent, std::ref(*this)); 
    this->dispatcherThread.join(); 

} 

我正在使用QtCreator來建立。並得到這個錯誤:

/home/eventqueue.o:-1: In function std::thread::thread<void (EventQueue::*)(), std::reference_wrapper<EventQueue> >(void (EventQueue::*&&)(), std::reference_wrapper<EventQueue>&&)': /usr/include/c++/4.9/thread:136: error: undefined reference to pthread_create' :-1: error: collect2: error: ld returned 1 exit status

問題是什麼?

我引用這個帖子: Storing an std::thread object as a class member

不過,我總是得到上述

+0

看來你沒有鏈接'pthread'。 – Jarod42

回答

3

你需要對並行線程庫鏈接描述的編譯錯誤。

使用GCC,我們使用-pthread選項執行此操作。

例如:g++ -pthread ...

+1

啊,我很愚蠢。雖然我使用的是QTCreator,所以修復方法是將這些標記放入.pro文件中:QMAKE_CXXFLAGS + = -std = C++ 0x -pthread LIBS + = -pthread – Ray

+1

我們不直接鏈接,而是使用用於編譯和鏈接GCC的'-pthread'選項。 –

+0

啊,沒錯。更新,謝謝。 –

相關問題