Pthread의 thread specific data에 대한 질문입니다.

익명 사용자의 이미지

-----------------------------------------------------------

#include

void * test2(void *);

static pthread_key_t r1_key;
static pthread_once_t r1_once = PTHREAD_ONCE_INIT;

void destructor(void *ptr)
{
sleep(10); <==== 주목!!!
printf("I am destructor addr %x\n", ptr);
free(ptr);
}

void once(void)
{
pthread_key_create(&r1_key, destructor);
}

main()
{
pthread_t tid[10];
pthread_t id;
int val = 3;
void **status;
int i, ret;

for ( i = 0; i < 10; i ++)
{
pthread_create((tid + i), NULL, test2, (void *)(tid + i));
}

for ( i = 0; i < 10; i ++ )
{
pthread_join(*(tid + i), status);
printf("thread return 1 val is %d\n", *(int *)*status);
}

}

void * test2(void *arg)
{
int *pt;
int *pid;

pid = (int *) arg;

pthread_once(&r1_once, once);
if ((pt = pthread_getspecific(r1_key)) == NULL)
{
pt = (int *)calloc(1, sizeof(int));
pthread_setspecific(r1_key, pt);
}

++*pt;
printf("pt addr %x val %d pid %d\n", pt, *pt, *pid);
// sleep(10);
return((void *)pt);
}

------------------------------------------------------------------

실행결과.

pt addr 8049ab8 val 1 pid 2
pt addr 8049ac8 val 1 pid 3
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 4
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 5
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 6
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 7
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 8
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 9
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 10
I am destructor addr 8049ac8
pt addr 8049ac8 val 1 pid 11
I am destructor addr 8049ac8
I am destructor addr 8049ab8
thread return 1 val is 1
thread return 1 val is 134519500
thread return 1 val is 134519500
thread return 1 val is 134519500
thread return 1 val is 134519500
thread return 1 val is 134519500
thread return 1 val is 134519500
thread return 1 val is 134519500
thread return 1 val is 134519500
thread return 1 val is 134519500

-----------------------------------------------------

의도 제가 알고 싶었던 것은 pthread_setspecific이나,
pthread_getspecific을 사용하였을 때 각 thread마다 다
른 메모리가 할당되는 것을 보고자 하였습니다.

그런데 불행이도 각 Thread가 항상 같은 address를 가르
키고 있는 것을 알게 되었습니다.

그 이유로 살펴보니, 위 destruct함수가 너무 빨리 불려
지는 이유로 항상 같은 메모리에 calloc함수가 리턴하게
됨을 알게 되었습니다.

그래서 저는 destructor함수에 address를 print하기 전에
sleep(10)을 넣어 free를 늦추면 각 thread마다 다른 메
모리를 할당하는 것을 볼수 있다고 생각했습니다.

질문 destructor함수에 sleep(10)을 넣었음에도 불구하
고 desctructor함수는 이 sleep(10)을 무시하고 결과를 먼
저 찍어버립니다.

왜 그럴까요?

혹시 이 destructor라는 함수가 시스템에서 불려지는 함수
이기 때문에 sleep(10)을 무시하는 것일까요?

아니면 다른 이유라도 있을까요?

댓글 달기

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