[완료] pthread_create()와 pthread_join()을 정확히 모르겠어요.

thslrtoo의 이미지

스레드 공부를 하고있는데 아래와 같은 코드가 예제로 있더군요.

void *t_function(void *data)
{
    while(1)
    {
       1초마다 출력
    }
}
 
int main()
{
    int a = 1;
    int b = 2;
 
    pthread_create(&p_thread[0], NULL, t_function, (void *)&a);
    pthread_create(&p_thread[1], NULL, t_function, (void *)&b);
 
    pthread_join(p_thread[0], (void **)&status);
    pthread_join(p_thread[1], (void **)&status);
}

많이 생략하고 필요한 부분만 써봤는데요.
pthread_create()를 호출하고 생성된 쓰레드는 바로 t_function() 함수를 실행합니다.
하지만 t_function() 함수는 무한 루프이므로 pthread_join()은 계속 기다리게 됩니다.
제가 궁금한건 저기서 pthread_join() 함수를 주석처리하면 t_function() 함수도 처리되지 않더군요.
근데 더 궁금한건 아래와 같은 코드를 작성해봤는데요.
(다 같고 main 부분만 살짝 바꿨습니다.)

int main()
{
    pthread_create(&p_thread[0], NULL, t_function, (void *)&a);
    pthread_create(&p_thread[1], NULL, t_function, (void *)&b);
 
    while (1)
    {
       무한 루프
    }
 
    //pthread_join(p_thread[0], (void **)&status);
    //pthread_join(p_thread[1], (void **)&status);
}

while 문을 추가하고 pthrad_join() 함수를 주석처리 했습니다.
그런데 이 소스는 제대로 동작하더라구요.
위 소스랑 똑같이 pthread_join()을 주석처리 했는데, 왜 결과가 다르게 나오죠??
pthread_create() 함수와 pthread_join()함수의 정체가 궁금하네요.

익명 사용자의 이미지

일단 man pthread_join을 해서 읽어보시고요.

main()에서 무한루프나 pthread_join()이 없으면 쓰레드 생성하자 마자 main()함수가 반환되겠죠?

그럼 프로세스 자체가 종료하니까 새로 생성된 쓰레드들은 돌아보기도 전에 강제로 사라지게 되겠죠?

klyx의 이미지

실행된 프로그램은 프로세스로 돌아가고, 각 스레드는 그 프로세스 안에서 돌아갑니다.
main함수가 돌아가는 스레드와 다른 스레드는 동시에 실행되므로 pthread_create는 해당 스레드의 종료를 기다리지 않고 바로 반환됩니다.
main이 끝나는 시점에서 프로세스가 끝나고, 그럼 프로세스안의 스레드도 모두 죽습니다.
해당 스레드의 종료를 기다리기 위해서 넣는게 pthread_join입니다.
만약 프로그램이 끝나도 별도로 계속 돌아가는 걸 원하는 거라면 스레드로 분리하는게 아니라 프로세스로 분리해야합니다.

thslrtoo의 이미지

두분 다 답변 감사합니다.
제가 멍청했네요.
당연히 메인 함수가 끝나버리니까 그런건데...^^;;

댓글 달기

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