2016-07-26 2 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

그러나 나는 항상

당신은 pthread와 라이브러리에 링크해야
+0

당신은 pthread''와 연결하지 않습니다. – Jarod42

답변

3

위에서 설명한 컴파일 오류가 발생합니다.

GCC의 경우 -pthread 옵션을 사용합니다. 예를 들어

: 것 같다 g++ -pthread ...

+1

어리석은 날. QTCreator를 사용하고 있었기 때문에 이러한 플래그를 .pro 파일에 저장하는 것이 수정되었습니다. QMAKE_CXXFLAGS + = -std = C++ 0x -pthread LIBS + = -pthread – Ray

+1

더 이상 직접 링크하지 않지만 '-pthread' 옵션을 사용하여 GCC를 컴파일하고 링크합니다. –

+0

아, 맞습니다. 업데이트되었습니다. 감사합니다. –