부모 쓰레드에서 detached 된 자식 쓰레드들의 종료 기다리기

숲으로의 이미지

부모 쓰레드에서 detached 된 자식 쓰레드들이 모두 종료하길 기다렸다가
부모자신도 종료되게끔 구현하고자 합니다.

일단 block 이든, non-block 이든, 비동기든 상관은 없습니다.

제가 구현한 벙법은
자식 쓰레드들에서 종료직전에 부모에게 pthread_kill()로 시그널을 보내주면
부모가 이를 받아서 모든 자식이 시그널을 보냈다 싶음 자신도 종료하는 방식인데요.
부모가 기다리기위해서는 while루프를 돌며 sigwait() 나, 비동기적으로 sigaction() 을 써서 구현해 봤습니다.

그런데 간혹 일부 자식으로부터 시그널을 받지 못하는 경우가 발생합니다.
아니 정확히는, 한 자식으로부터 온 시그널을 처리하는 동안 또다른 자식으로부터 시그널이 도착하면
인식하지 못하는 듯 합니다.

모든 자식의 시그널을 완벽히 받아 처리할 수 있는 방법이 없을까요?
혹은 꼭 시그널을 이용하지 않더라도 가능한 방법은 없습니까?

cwryu의 이미지

pthread_join() 이요? (detach하지 말아야 하지만...)

prether의 이미지

설령 signal을 무시하거나 블록시키지 않아도
signal이 pending된 상태에서 다시 동일 시그널이 올경우 늦게 도착한 넘은
그대로 버려집니다.

즉 (보낸 시그널 수 != 받는 시그널 수)

이걸 해결하기 위해 만든게 Real-time signal 입니다.

그 방법 외에
현재 자식 쓰레드 수를 카운트하는
전역 변수를 하나 만들고 그 전역변수가 zero로 떨어지는 순간
(물론 동기화를 위해 MUTEX를 사용하고)
exit()를 하면 간단하지 않을까요?

/***************************************
Being the one is just like being in love.
***************************************/

superkkt의 이미지

main 쓰레드에서 모든 작업을 마친 후 pthred_exit()을 호출하면 됩니다. 그럼 나머지 모든 쓰레드가 종료할때까지 프로그램이 종료되지 않습니다.

음.. 질문을 다시보니 부모 쓰레드가 main 쓰레드가 아닐수도 있겠군요. 그러면 안될텐데..

======================
BLOG : http://superkkt.com

======================
BLOG : http://superkkt.com

댓글 달기

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