다음 구문을 봐주세요.

익명 사용자의 이미지

#include
#include

void* thrFunc(void *arg);
void main()
{
pthread_t thread;
void *ptr;
int a = 99;

if ( pthread_create( &thread , NULL , thrFunc , (void*)a ) < 0 )
perror( "create" );
if ( pthread_join( thread , &ptr ) < 0 )
perror( "join" );
fprintf( stderr , "main thread , ret %d\n" , *(int*)ptr );

//pthread_exit( ptr );
}

void* thrFunc(void *arg)
{
int ret;
fprintf( stderr , "in Thread, %d\n" , (int)arg );
ret = (int) arg;
pthread_exit( (void*)&ret );
}

1. thrFunc함수내에서
리턴값을 auto변수로 했는데요.
이것이 정확한지요?
2. 또한, 메인부분에
pthrea_join 두번째 인수를 (선언)void **ptr ---->(사용) ptr 이렇게 선
언해놓고 사용해야할지
아니면 (선언)void *ptr ----> (사용) &ptr 로 사용하는것이 맞는지 궁
금합니다.

익명 사용자의 이미지


1. thrFunc함수내에서
리턴값을 auto변수로 했는데요.
이것이 정확한지요?

auto 변수에 대한 포인터를 리턴하시면 안됩니다.
pthread_exit((void *)ret);
식으로 강제로 형변환시키시고 auto 변수의 값을 리턴하세요.
만일 지금과 같이 int 값 하나를 넘기는 것이 아니라
넘기는 값이 많은 경우에는 malloc() 등으로 메모리를 할당받아
여기에 대한 포인터를 넘기세요.

2. 또한, 메인부분에
pthrea_join 두번째 인수를 (선언)void **ptr ---->(사용) ptr 이렇 게 선
언해놓고 사용해야할지
아니면 (선언)void *ptr ----> (사용) &ptr 로 사용하는것이 맞는지 궁
금합니다.

void *ptr; 로 정의하신 후에 &ptr로 쓰시는 것이 맞습니다.
아니면
int ret_val; 로 하신 후 (void **)&ret_val;
식으로 하셔도 상관없습니다.
중요한 것은 할당된 공간에 대한 포인터를 전달하는 것입니다.

만일 int가 아니라 다른 구조를 리턴한다고 하면,

struct record *ptr;
로 정의하시고, (void **)&ptr 식으로 join에 전달하시면 되겠죠.
void *ptr 로 정의하고 사용시마다 (struct record *)로 캐스팅하시는것보
다 편리할 것입니다. 물론 thread쪽에서는 1번에서 언급한 것과 같이 메모
리를 할당해서 넘겨야겠죠.

수고하세요,,

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.