pthread사용해서 여러개 스레드를 돌릴때 스레드 핸들을 스레드 갯수에 맞추어야 할까요?

oblab의 이미지

스레드 핸들을 가지고 스레드를 제어하게 되는데요
main함수에서 pthread_t 변수를 선언해서 사용하는데
정석(?)으로는 스레드당 하나의 스레드 핸들을 사용하는걸로 알고 있는데요
두 가지 방식이 크게 영향이 있을까요?

예제 : 스레드 핸들을 하나로 10개의 스레드를 생성
int main(int argc, char **argv){
int i=10;
pthread_t thread;
for(i = 0; i < th_num; i++)
{
pthread_create(&th_test, NULL, thread, (void *)&i) != 0
...
}
prhead_detach(thread);
return 0;
}

예제 : 스레드 핸들을 10개로 10개의 스레드를 생성
int main(int argc, char **argv){
int i=10;
pthread_t thread[10];
for(i = 0; i < th_num; i++)
{
pthread_create(&th_test, NULL, thread[i], (void *)&i) != 0
...
}

for(i = 0; i < th_num; i++)
{
pthread_detach(thread[i]);
}
return 0;
}

chanik의 이미지

첫번째 예제는 마지막으로 만든 스레드만 detach 하게 됩니다.
pthread_detach() 호출을 for loop 안으로 이동시키면 두 번째 예제와 같은 동작을 하겠네요.

아니면 아예 pthread_create()할때 PTHREAD_CREATE_DETACHED 상태로 만들면
pthread_detach() 일일이 호출할 필요도 없고요.

그런데, pthread_join() 없이 멀티스레드 프로그래밍을 하거나
위 예제처럼 pthread_detach()를 통해 pthread_join() 불가능한 상태로 만들게 되면
해당 프로세스가 종료될 때 각 스레드들은 실행중 갑작스럽게 종료되게 됩니다.
작업을 우아하게 마무리할 수 없게 되는 것이죠.

oblab의 이미지

좋은 팁 감사합니다.

oblab의 이미지

스레드핸들 갯수의 차이가 궁금했는데 소스가 잘못된것 같아서 수정했습니다.

예제 : 스레드 핸들을 하나로 10개의 스레드를 생성
int main(int argc, char **argv){
int i;
int th_test=10;
pthread_t thread;
for(i = 0; i < th_num; i++)
{
pthread_create(&th_test, NULL, thread, (void *)&i) != 0
...
prhead_detach(thread);
}
return 0;
}

예제 : 스레드 핸들을 10개로 10개의 스레드를 생성
int main(int argc, char **argv){
int i;
int th_num=10
pthread_t thread[10];
for(i = 0; i < th_num; i++)
{
pthread_create(&th_test, NULL, thread[i], (void *)&i) != 0
...
pthread_detach(thread[i]);
}

return 0;
}

chanik의 이미지

[code lang=c]
... C 코드 ...
[/code]

이렇게 하시면 문법강조 효과도 나고, 들여쓰기 등 레이아웃도 잘 보존됩니다.
lang은 따로 지정 안해도 되고,
언어에 따라 c, cpp, python, perl, bash, sql, make, ... 등 적절히 바꿔 넣어도 됩니다.

[미리보기]버튼으로 글의 모양을 미리 확인할 수 있으니 참고하시고요.
그 버튼 위에 있는 "입력형식" 열면 나오는 BBCode (http://kldp.org/filter/tips/1#filter-bbcode-0) 도 큰 도움이 됩니다.

antaran의 이미지

간단합니다. 핸들링하고자 하는 스레드 갯수만큼 쓰시는 것이 정석입니다.
예제 코드상에서 thread의 handler를 쓰실 일이 당장 눈에 보이지 않으니 체감되지 않으시는 겁니다.

int fd[10] ;
for(int i= 0 ; i < 10 ; ++i)
{
    fd[i]= open(...) ;
    // fd[i]가 없으면 어떻게...? read(fd[i]...) & write(fd[i]...) ;
 
    close(fd[i]) ;
}

좋은 하루 되세요.

댓글 달기

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