pthread 예제프로그램이 컴파일이 안되네요.

kainhide의 이미지

음 책보며 공부 하는데 사정상 책을 처음 부터 볼수가 없어 바로 Thread 프로그램으로 넘어 왓습니다.
MFC환경에 익숙해져있다 리눅스에서 pthread 라는 것을 사용해볼려고 책의 예제 프로그램을 만들었는데 컴파일이 안됩니다.
이것저것 해보아도 -_- 잘모르겠어요..한번 봐주세요.

///////////////////////////////////////////////////////////////////////////

 
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
 
void *thread_function(void *arg);
 
char message[] = " Sibal World ";
 
int main()
{
	int res;
	pthead_t a_thread;
	void *thread_result;
 
	res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
	if( res !=0)
		{
			perror("Thread creation failed");
			exit(EXIT_FAILURE);
		}
 
	printf("Wating for thread to finish...\n");
	res = pthread_join(a_thread,&thread_result);
	if( res != 0 )
		{
			perror("Thread join failed");
			exit(EXIT_FAILURE);
		}
 
	printf("thread joined , it returned %s \n", (char *)thread_result);
	printf("message is now %s \n",message);
	exit(EXIT_SUCCESS);
}
 
void *thread_function(void *arg)
{
 
	printf("thread_function is running. Argument was %s \n", (char *)arg);
	sleep(3);
	strcpy(message,"Bye!");
	pthread_exit("Thank you for CPU time");
}

//////////////////////////////////////////////////////////////////////////////////////////

위 프로그램이구요

컴파일은

cc -D_REENTRANT thread1.cxx -o thread2 -lpthread

이명령어로 했습니다.

근데 에러가 발생해요 마지막 void *thread_function(void *arg) 이부분에서 나는데요
in function 'void* thread_function(void*)':
Line43:invalid conversion form 'const void*) to 'void*)

도움좀 부탁드립니다.

wariua의 이미지

컴파일러의 호소 대로 43행을 보시면 됩니다.

  pthread_exit("Thank you for CPU time");

"man pthread_exit" 해보면 아시겠지만 pthread_exit()의 인자인 retval의 타입은 void *입니다. 그런데 여기에 const char *인 "Thank you ..."를 넘겨주니 타입 확인에 철저한 C++ 컴파일러가 이를 보고 참지 못하여 잘못된 변환(invalid conversion)이라고 외치는 것입니다. pthread_join()을 호출한 스레드에게 전해줄 적당한 반환값을 넘겨주시면 됩니다.

& <code>...</code> 태그를 쓰시면 코드를 좀더 깔끔하게 포스팅 하실 수 있습니다 :)
----
$PWD `date`

$PWD `date`