2012-10-29 3 views
1

Possible Duplicate:
pthread Function from a Class이는 pthread_create

를 사용할 때 오류가 나는 C 상당히 새로운 오전 ++ 나는 TCP에 대한 프로젝트를하고있는 중이 야.

내가 인터넷 검색을 할 수 있도록 스레드를 만들어야합니다. http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

내가 오류의 구문을 따라야하지만, 발생 : 유형의 인수 '무효 *을 (NS3는 :: TcpSocketBase는 :)()'

'무효 * () (무효를)'과 일치하지 않습니다 코드 :

tcp-socket-base.h: 
class TcpSocketBase : public TcpSocket 
{ 
public: 
... 
void *threadfunction(); 
.... 
} 




tcp-socket-base.cc: 

void 
*TcpSocketBase::threadfunction() 
{ 
//do something 
} 



..//the thread was create and the function is called here 
pthread_t t1; 
int temp = pthread_create(&t1, NULL, ReceivedSpecialAck, NULL); //The error happens here 
return; 
... 

어떤 도움을 주시면 감사하겠습니다. 감사!

편집 :

은 내가 조언을 가져다가이 쓰레드에게 비 멤버 함수를합니다.

namespaceXXX{ 

void *threadfunction() 


int result = pthread_create(&t1, NULL, threadfunction, NULL); 
     NS_LOG_LOGIC ("TcpSocketBase " << this << " Create Thread returned result: " << result); 

void *threadfunction() 
{ 
..... 
} 


} 

하지만 대신이 오류 가지고 :

의 인수 3 초기화 '의 INT는 pthread_create (가 pthread_t의 *, const를 pthread_attr_t의 *를 무효 * () (무효), 무효 *)'[-fpermissive ]

+1

또한 boost :: thread를 사용하는 것이 좋습니다. 그러면 인생이 훨씬 쉬워 질 것입니다. – fluffy

답변

2

, 간단한 예는 다음과 같습니다

#include <cstdio> 
#include <string> 
#include <iostream> 

#include <pthread.h> 

void* print(void* data) 
{ 
    std::cout << *((std::string*)data) << "\n"; 
    return NULL; // We could return data here if we wanted to 
} 

int main() 
{ 
    std::string message = "Hello, pthreads!"; 
    pthread_t threadHandle; 
    pthread_create(&threadHandle, NULL, &print, &message); 
    // Wait for the thread to finish, then exit 
    pthread_join(threadHandle, NULL); 
    return 0; 
} 

더 나은 대안은, 당신이 할 수 있다면, 새로운 C++ (11) thread library을 사용하는 것입니다. 템플릿을 사용하는보다 간단한 RAII 인터페이스이므로 클래스 멤버 함수 (this thread 참조)를 비롯하여 모든 함수를 스레드에 전달할 수 있습니다.

그런 다음, 위의 exmaple이로 단순화 : 당신은 그냥 직접 데이터를 전달할 수있는 방법을

#include <cstdio> 
#include <string> 
#include <iostream> 

#include <thread> 

void print(std::string message) 
{ 
    std::cout << message << "\n"; 
} 

int main() 
{ 
    std::string message = "Hello, C++11 threads!"; 
    std::thread t(&print, message); 
    t.join(); 
    return 0; 
} 

주 - 필요하지 않습니다에와 void*에서 캐스트.

+0

하지만이 오류가 있습니다 : 'void (*)()'에서 'void * (*) (void *)'로의 변환이 잘못되었습니다 [-fpermissive] – user1701840

+0

컴파일 할 수 있도록 예제를 수정했습니다. 'pthread_create'에 넘겨주는 함수는 void 포인터를 인수로 사용하여 데이터를 스레드로 전달할 수 있습니다. –

+0

(void * date)이 아니어야합니까? – user1701840

2

클래스의 멤버 함수를 pthread_create 함수에 전달하려고합니다. 스레드 함수는 다음과 같은 서명이있는 비 멤버 함수 여야합니다.

void *thread_function(void *ptr); 
+0

내 헤더 파일에 threadfunction을 선언해서는 안된다는 뜻입니까? – user1701840

+0

그게 맞아. 자습서에서는 다음과 같이 멤버 함수가 아닌 함수를 사용하는 예제를 볼 수 있습니다. 멤버 함수는 정적 멤버 함수가 아니라면 클래스의 인스턴스를 호출해야합니다. 클래스의 멤버 함수가 아닌 독립 실행 형 함수를 사용해보십시오. 귀하의 질문에 대한 의견에 중복 질문에 대한 링크를 참조하십시오 – mathematician1975

+0

내 스레드 함수가 클래스의 일부 함수를 호출해야 할 경우 어떻게됩니까? – user1701840

0

정적 함수를 선언하면 컴파일됩니다. 당신의 pthreads를 계속 사용하려는 경우

관련 문제