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`

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.