´Ù¾çÇÑ ¿¹Á¦·Î ¾Ë¾Æº¸´Â Pthread
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

Contents

1 Posix Thread Example
2 pthread ¿¹Á¦1
2.1 pthread1.c ¼Ò½ºÄÚµå
2.2 pthread1 ½ÇÇà°á°ú
3 pthread ¿¹Á¦2
3.1 pthread2.c ¼Ò½ºÄÚµå
3.2 pthread2 ½ÇÇà°á°ú
4 pthread ¿¹Á¦3
4.1 pthread3.c ¼Ò½ºÄÚµå
4.2 pthread3 ½ÇÇà°á°ú
5 pthread ¿¹Á¦4
5.1 pthread4 ¼Ò½ºÄÚµå
5.2 pthread4 ½ÇÇà°á°ú
6 ÂüÁ¶»çÀÌÆ®


1 Posix Thread Example


ÀÛ¼ºÀÚ: mwyun(¸Û)

¾²·¹µå »ý¼ºÈÄ ÀÚµ¿ Á¾·áÇÏ´Â °£´ÜÇÑ ¿¹Á¦ÀÔ´Ï´Ù.

2 pthread ¿¹Á¦1


2.1 pthread1.c ¼Ò½ºÄÚµå

#include <stdio.h> 
#include <string.h> 
#include <pthread.h> 
 
pthread_t threads[5]; 
int done[5]; 
 
void *thread_main(void *); 
 
int main(void) 
{ 
    int i; 
    int rc; 
    int status; 
     
    printf("pid=%d\n", getpid()); 
     
    for (i = 0; i < 5; i++) 
    {     
        done[i] = 0; 
        pthread_create(&threads[i], NULL, &thread_main, (void *)i); 
        printf("%d, %d\n", i, threads[i]); 
    } 
 
    for (i = 4; i >= 0; i--) 
    { 
        done[i] = 1; 
             rc = pthread_join(threads[i], (void **)&status); 
        if (rc == 0) 
        { 
            printf("Completed join with thread %d status= %d\n",i, status); 
        } 
        else 
        { 
            printf("ERROR; return code from pthread_join() is %d, thread %d\n", rc, i); 
                          return -1; 
        } 
    } 
 
    return 0; 
} 
 
void *thread_main(void *arg) 
{ 
    int i; 
    double result=0.0; 
 
    printf("therad: %d, %d\n", (int)arg, getpid()); 
 
    while (!done[(int)arg]) 
    { 
       for (i=0; i < 1000000; i++) 
          { 
               result = result + (double)random(); 
          } 
          printf("thread: %d, result = %e\n", (int)arg, result); 
    } 
 
    pthread_exit((void *) 0); 
} 
 

2.2 pthread1 ½ÇÇà°á°ú

[mwyun@ns mwyun]$ gcc -o pthread1 pthread1.c -lpthread 
[mwyun@ns mwyun]$ ./pthread1 
pid=2790 
therad: 0, 2790 
0, 1082346288 
therad: 1, 2790 
1, 1090738992 
therad: 2, 2790 
2, 1099131696 
therad: 3, 2790 
3, 1116957488 
therad: 4, 2790 
4, 1125350192 
thread: 2, result = 1.073082e+15 
thread: 1, result = 1.074421e+15 
thread: 4, result = 1.073731e+15 
Completed join with thread 4 status= 0 
thread: 0, result = 1.073430e+15 
thread: 3, result = 1.074102e+15 
Completed join with thread 3 status= 0 
thread: 2, result = 2.146727e+15 
Completed join with thread 2 status= 0 
thread: 1, result = 2.147847e+15 
Completed join with thread 1 status= 0 
thread: 0, result = 2.148343e+15 
Completed join with thread 0 status= 0 
[mwyun@ns mwyun]$ 
 

»ý¼ºµÈ ¾²·¹µå°¡ Á¤»óÀûÀ¸·Î µ¿ÀÛÇϰí pthread_exitÇÔ¼ö¸¦ ÅëÇØ¼­ 0°ªÀ» ¸®ÅÏÇϰí Á¾·áÇÏ¿´À¸¹Ç·Î pthread_joinÇÔ¼ö¿¡¼­ status°ª 0ÀÌ Ãâ·ÂµÈ´Ù.


3 pthread ¿¹Á¦2


¾²·¹µå »ý¼ºÈÄ °­Á¦·Î Á¾·á½ÃŰ´Â °£´ÜÇÑ ¿¹Á¦ÀÔ´Ï´Ù.

3.1 pthread2.c ¼Ò½ºÄÚµå


#include <stdio.h> 
#include <string.h> 
#include <pthread.h> 
 
pthread_t threads[5]; 
int done[5]; 
 
void *thread_main(void *); 
 
int main(void) 
{ 
    int i; 
    int rc; 
    int status; 
     
    printf("pid=%d\n", getpid()); 
 
    for (i = 0; i < 5; i++) 
    {     
        done[i] = 0; 
        pthread_create(&threads[i], NULL, &thread_main, (void *)i); 
        printf("%d, %d\n", i, threads[i]); 
    } 
 
    for (i = 4; i >= 0; i--) 
    { 
        rc = pthread_cancel(threads[i]); // °­Á¦Á¾·á 
        if (rc == 0) 
        { 
            // ÀÚµ¿Á¾·á 
            rc = pthread_join(threads[i], (void **)&status); 
            if (rc == 0) 
            { 
                printf("Completed join with thread %d status= %d\n",i, status); 
            } 
            else 
            { 
                printf("ERROR; return code from pthread_join() is %d, thread %d\n", rc, i); 
                     return -1; 
            } 
        } 
    } 
 
    return 0; 
} 
 
void *thread_main(void *arg) 
{ 
    int i; 
    double result=0.0; 
 
    printf("therad: %d, %d\n", (int)arg, getpid()); 
 
    while (!done[(int)arg]) 
    { 
      for (i=0; i < 1000000; i++) 
        { 
          result = result + (double)random(); 
      } 
        printf("thread: %d, result = %e\n", (int)arg, result); 
    } 
 
    pthread_exit((void *) 0); 
} 
 

3.2 pthread2 ½ÇÇà°á°ú

[mwyun@ns mwyun]$ gcc -o pthread2 pthread2.c -lpthread 
[mwyun@ns mwyun]$ ./pthread2 
pid=2845 
therad: 0, 2845 
0, 1082346288 
therad: 1, 2845 
1, 1090738992 
therad: 2, 2845 
2, 1099131696 
therad: 3, 2845 
3, 1116957488 
therad: 4, 2845 
4, 1125350192 
thread: 3, result = 1.073353e+15 
thread: 4, result = 1.074336e+15 
thread: 0, result = 1.073328e+15 
Completed join with thread 4 status= -1 
thread: 1, result = 1.074552e+15 
thread: 2, result = 1.073090e+15 
thread: 3, result = 2.147247e+15 
Completed join with thread 3 status= -1 
thread: 0, result = 2.147519e+15 
thread: 1, result = 2.147391e+15 
thread: 2, result = 2.147080e+15 
Completed join with thread 2 status= -1 
thread: 0, result = 3.221195e+15 
thread: 1, result = 3.221001e+15 
Completed join with thread 1 status= -1 
thread: 0, result = 4.295871e+15 
Completed join with thread 0 status= -1 
[mwyun@ns mwyun]$ 
 

pthread_cancel ÇÔ¼ö¸¦ »ç¿ëÇÏ¿© °­Á¦ÀûÀ¸·Î Á¾·áÇÏ¿´À¸¹Ç·Î pthread_joinÇÔ¼ö¿¡¼­ status°ª -1ÀÌ Ãâ·ÂµÈ´Ù.

4 pthread ¿¹Á¦3


¾²·¹µå »ý¼ºÈÄ ¾²·¹µå¸¦ ºÐ¸®½ÃŰ´Â °£´ÜÇÑ Ã¹¹øÂ° ¿¹Á¦ÀÔ´Ï´Ù.

4.1 pthread3.c ¼Ò½ºÄÚµå

#include <stdio.h> 
#include <string.h> 
#include <pthread.h> 
 
pthread_t threads[5]; 
int done[5]; 
 
void *thread_main(void *); 
 
int main(void) 
{ 
    int i; 
    int rc; 
    int status; 
     
    printf("pid=%d\n", getpid()); 
 
    for (i = 0; i < 5; i++) 
    {     
        done[i] = 0; 
        pthread_create(&threads[i], NULL, &thread_main, (void *)i); 
        printf("%d, %d\n", i, threads[i]); 
    } 
 
    for (i = 4; i >= 0; i--) 
    { 
        done[i] = 1; 
        rc = pthread_join(threads[i], (void **)&status); /* detach thead¿¡¼­´Â »ç¿ëÇÒ ÇÊ¿ä ¾ø´Ù. */ 
        if (rc == 0) 
        { 
            printf("Completed join with thread %d status= %d\n",i, status); 
        } 
        else 
        { 
            printf("ERROR; return code from pthread_join() is %d, thread %d\n", rc, i); 
                 return -1; 
        } 
    } 
 
    return 0; 
} 
 
void *thread_main(void *arg) 
{ 
    int i; 
    double result=0.0; 
 
    pthread_detach(pthread_self()); /* ¾²·¹µå ºÐ¸® */ 
 
    printf("therad: %d, %d\n", (int)arg, getpid()); 
 
    while (!done[(int)arg]) 
    { 
            for (i=0; i < 1000000; i++) 
           { 
                 result = result + (double)random(); 
           } 
           printf("thread: %d, result = %e\n", (int)arg, result); 
    } 
 
    pthread_exit((void *) 0); 
} 
 

4.2 pthread3 ½ÇÇà°á°ú

[mwyun@ns mwyun]$ gcc -o pthread3 pthread3.c -lpthread 
[mwyun@ns mwyun]$ ./pthread3 
pid=2949 
therad: 0, 2949 
0, 1082346288 
therad: 1, 2949 
1, 1090738992 
therad: 2, 2949 
2, 1099131696 
therad: 3, 2949 
3, 1116957488 
therad: 4, 2949 
4, 1125350192 
ERROR; return code from pthread_join() is 22, thread 4 
[mwyun@ns mwyun]$ 
 

°á°ú¸¦ º¸¸é ¿¡·¯°¡ ¹ß»ýÇÏ´Â °ÍÀ» º¼ ¼ö ÀÖ´Ù.

ÂüÁ¶»çÀÌÆ®ÀÇ Thread Management > Joining Threads¸¦ º¸¸é

It is impossible to join a detached thread (discussed next)

À̶ó°í ³ª¿Â´Ù.

Áï, detached threadµÈ °ÍÀº joinÀÌ ºÒ°¡´ÉÇϹǷΠpthread_joinÇÔ¼ö¸¦ »ç¿ëÇÒ Çʿ䰡 ¾ø´Ù.

Âü°í·Î pthread_joincÀº waitpid¿Í ºñ±³µÉ ¼ö ÀÖ´Ù.

5 pthread ¿¹Á¦4


¾²·¹µå »ý¼ºÈÄ ¾²·¹µå¸¦ ºÐ¸®½ÃŰ´Â °£´ÜÇÑ µÎ¹øÂ° ¿¹Á¦ÀÔ´Ï´Ù.

pthread3.c¿¡¼­ pthread_joinc¿¡ °ü·ÃµÈ Äڵ带 »èÁ¦ÇÏ¿´½À´Ï´Ù.

5.1 pthread4 ¼Ò½ºÄÚµå

#include <stdio.h> 
#include <string.h> 
#include <pthread.h> 
 
pthread_t threads[5]; 
int done[5]; 
 
void *thread_main(void *); 
 
int main(void) 
{ 
    int i; 
    int rc; 
    int status; 
     
    printf("pid=%d\n", getpid()); 
 
    for (i = 0; i < 5; i++) 
    {     
        done[i] = 0; 
        pthread_create(&threads[i], NULL, &thread_main, (void *)i); 
        printf("%d, %d\n", i, threads[i]); 
    } 
 
    for (i = 4; i >= 0; i--) 
    { 
        done[i] = 1; 
    } 
     
    /* ¾²·¹µåµéÀÌ ½ÇÇàÇϰí Á¾·áÇÒ ¶§ ±îÁö Àá½Ã ±â´Ù¸°´Ù. 
       sleepÀ» ÁÖÁö ¾ÊÀ¸¸é ¸ÞÀÎÀÌ Á¾·áµÇ¹Ç·Î ÀÚµ¿À¸·Î »ý¼ºµÈ ¾²·¹µåµµ °­Á¦Á¾·áµÇ¹Ç·Î 
       ¿Ã¹Ù¸¥ Å×½ºÆ® ÁøÇàµÇÁö ¾Ê¾Æ¼­ ÀÌ´Ù. */ 
    sleep(5); 
 
    return 0; 
} 
 
void *thread_main(void *arg) 
{ 
    int i; 
    double result=0.0; 
 
    pthread_detach(pthread_self()); 
 
    printf("therad: %d, %d\n", (int)arg, getpid()); 
 
    while (!done[(int)arg]) 
    { 
            for (i=0; i < 1000000; i++) 
           { 
                 result = result + (double)random(); 
           } 
           printf("thread: %d, result = %e\n", (int)arg, result); 
    } 
 
    printf("thread %d terminated....\n", (int)arg); 
    pthread_exit((void *) 0); 
} 
 

5.2 pthread4 ½ÇÇà°á°ú

[mwyun@ns mwyun]$ gcc -o pthread4 pthread4.c -lpthread 
[mwyun@ns mwyun]$ ./pthread4 
pid=3140 
therad: 0, 3140 
0, 1082346288 
therad: 1, 3140 
1, 1090738992 
therad: 2, 3140 
2, 1099131696 
therad: 3, 3140 
3, 1116957488 
therad: 4, 3140 
4, 1125350192 
thread: 0, result = 1.074314e+15 
thread 0 terminated.... 
thread: 4, result = 1.073499e+15 
thread 4 terminated.... 
thread: 1, result = 1.073339e+15 
thread 1 terminated.... 
thread: 2, result = 1.074428e+15 
thread 2 terminated.... 
thread: 3, result = 1.073145e+15 
thread 3 terminated.... 
[mwyun@ns mwyun]$ 
 

6 ÂüÁ¶»çÀÌÆ®

  1. http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html
  2. pthread API ·¹ÆÛ·±½º
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.