我想創建一個線程並保持對它的引用作爲類的標記,並且線程調用該類的一個方法。這裏是代碼:創建一個線程作爲一個類的數據成員
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
不過,我總是得到上述
看來你沒有鏈接'pthread'。 – Jarod42