Do You Coding?
[function] pthread_join ํจ์ ์์๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (Linux)
NAME
pthread_join - join with a terminated thread
SYNOPSIS
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);
Compile and link with -pthread.
DESCRIPTION
The pthread_join() function waits for the thread specified by thread to terminate.
If that thread has already terminated, then pthread_join() returns immediately.
The thread specified by thread must be joinable. If retval is not NULL, then pthread_join()
copies the exit status of the target thread (i.e., the value that the target thread supplied to
pthread_exit(3)) into the location pointed to by retval. If the target thread was canceled, then
PTHREAD_CANCELED is placed in the location pointed to by retval.
If multiple threads simultaneously try to join with the same thread, the results are undeโ fined.
If the thread calling pthread_join() is canceled, then the target thread will remain joinable
(i.e., it will not be detached).
RETURN VALUE
On success, pthread_join() returns 0; on error, it returns an error number.
ERRORS
EDEADLK
A deadlock was detected (e.g., two threads tried to join with each other);
or thread specifies the calling thread.
EINVAL
thread is not a joinable thread.
EINVAL
Another thread is already waiting to join with this thread.
ESRCH
No thread with the ID thread could be found.
๐ ํจ์ ์ค๋ช
์ด์ pthread_create ํจ์์ ์ด์ด, ์ค๋ ๋ ๊ด๋ จ ํจ์์ธ pthread_join ํจ์์ ๋ํด์ ์์๋ณด๋๋ก ํ๊ฒ ๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก pthread.h ๋ฅผ include ํ์ฌ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ,
์ด ํจ์๋ ์๋ธ ์ค๋ ๋๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ๋ฉ์ธ ์ค๋ ๋๊ฐ ๊ธฐ๋ค๋ฆฌ๊ฒ ํด์ฃผ๋ ์ญํ ์ด๋ค.
๋จ์ํ main๋ฌธ์์ pthread_create๋ฅผ ์ด์ฉํด ์ค๋ ๋๋ฅผ ๋ง๋ค์์๋,
๋ง๋ ์ค๋ ๋๋ฅผ '์๋ธ ์ค๋ ๋', main๋ฌธ์ '๋ฉ์ธ ์ค๋ ๋' ๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
์ฌ๊ธฐ์, sleep์ฒ๋ผ ๊ธฐ๋ค๋ฆฌ๋ ๋ถ๋ถ์ main์์ ๋ง๋ค์ง ์์๋ค๋ฉด,
ํด๋น ์๋ธ ์ค๋ ๋๊ฐ ์ฒ๋ฆฌ๋ฅผ ๋ค ์ํํ์ง ๋ชปํ๊ณ ์ข ๋ฃ๋๋ ์ํฉ์ด ๋ฐ์ํ ์ ์๋๋ฐ,
์ด ์ํฉ์ ๋ฐฉ์งํ๊ณ ์๋ธ ์ค๋ ๋์ ๊ฒฐ๊ณผ์ ๋ฐ๋ฅธ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ข ๋ฃ๊น์ง ๋๊ธฐํด์ฃผ๋ ํจ์๋ก pthread_join์ด ์ฐ์ธ๋ค.
๋ํ, ์ข ๋ฃ๋ฅผ ๋๊ธฐํ๋ฉด์ ์๋ธ ์ค๋ ๋์ ๋ฆฌํด ๊ฐ๋ ๋ฐ์ ์ฒ๋ฆฌ๋ ํด์ค๋ค.
int pthread_join(pthread_t thread, void **retval);
ํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ์ดํด๋ณด๋๋ก ํ์.
pthread_t thread : ์ข ๋ฃ๋ฅผ ๋๊ธฐํ๊ณ , ๋ฆฌํด ๊ฐ์ ๋ฐ์ ์ค๋ ๋์ ID ๊ฐ์ด๋ค.
pthread_create()๋ฅผ ์ํํ์ฌ ์ค๋ ๋๊ฐ ์์ฑ๋๊ณ ๋์จ ํด๋น ID ๊ฐ์ ๋์ ํ๋ฉด ๋๋ค.
void **retval : ์ค๋ ๋์์ ๋ฆฌํด๊ฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก void * ํ์์ผ๋ก ๋ฆฌํดํ๊ฒ ๋๋๋ฐ,
์ด ๊ฐ์ call by reference ๋ก ์ฃผ์ ๊ฐ์ ๋ฐ์ ๊ฐ์ ธ์จ๋ค.
(void ํ์์ ์ฌ์ฉํ๋ ์ด์ ๋ ์ํ๋ ํํ๋ก ์บ์คํ ํ๊ธฐ ์ํด์์ด๋ค.)
๊ทธ๋ฆฌ๊ณ pthread_join์ ๋ฐํ ๊ฐ์ ์ฑ๊ณต์ 0 / ์คํจ์ Error number๋ฅผ ๋ฐํํ๋ค.
์ถ๊ฐ์ ์ผ๋ก, ํ๋ก์ธ์ค์ ๋น๊ตํ์ ๋, ์ด pthread_join ํจ์๋ wait ๊ณ์ด์ ํจ์์ ๋น์ทํ ์ญํ ์ ํ๋ค.
(wait : ๋ถ๋ชจ ํ๋ก์ธ์ค๊ฐ ์์ ํ๋ก์ธ์ค์ ์ข ๋ฃ๋ฅผ ๋๊ธฐํ๋ค.)
pthread_create ์ ํจ๊ป ์ฌ์ฉํ์ฌ ์์๋ฅผ ํ๋ฒ ๋ง๋ค์ด๋ณด์.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *thread_func(void *arg)
{
int i = *((int*) arg);
int c = *((int*) arg) + 5;
while(i <= c)
{
sleep(1);
printf("running thread %d\n", i);
i ++;
}
return (NULL);
}
int main(void)
{
pthread_t thread_ID;
int arg = 0;
if(pthread_create(&thread_ID, NULL, thread_func, (void *)&arg) != 0)
exit(1);
//pthread_join(thread_ID, NULL);
printf("end\n");
return (0);
}
์ถ๋ ฅ - std_output)

์์ ์ฝ๋๋ main์์ pthread_create ํจ์๋ฅผ ์ฌ์ฉํ๊ณ , ํ๋์ ํจ์๋ฅผ ์คํํ๋ ์ค๋ ๋๋ฅผ ๋ง๋ค์๋ค.
๊ทธ๋ฌ๋ main ์ค๋ ๋์์๋ ํด๋น ์ค๋ ๋๊ฐ ์คํํ๋ ๋์ ๊ธฐ๋ค๋ฆฌ๋ ๋ถ๋ถ์ด ์กด์ฌํ์ง ์๊ณ ,
thread_func์ด ์คํ๋์ง ๋ชปํ๊ณ ๋ฐ๋ก end๋ง ์ถ๋ ฅํ๊ณ main ์ค๋ ๋๊ฐ ์ข ๋ฃ๋๊ฒ ๋๋ค.
์ด์ ์ pthread_create ๊ฒ์๊ธ์์๋ sleep์ ํตํด์ main ์ค๋ ๋ ์ข ๋ฃ๋ฅผ ๋ง์์คฌ์ง๋ง,
์์ ์ฝ๋์์๋ ๊ทธ ๋ถ๋ถ์ ์ง์ ์ผ๋ฏ๋ก main ์ค๋ ๋๊ฐ ๊ธฐ๋ค๋ฆด ์ ์๋ ์ํ์ด๋ค.
๊ทธ๋ผ ์ฃผ์์ผ๋ก ์ฒ๋ฆฌํด๋ pthread_join์ ๋ฃ์ด์ค๋ค๋ฉด ์ด๋ป๊ฒ ๋๋์ง ์ดํด๋ณด์.
-> pthread_join(thread_ID, NULL); ๋ถ๋ถ ์ฃผ์ ์ฒ๋ฆฌ๋ฅผ ์ง์ ์ ๊ฒฝ์ฐ

ํจ์ ์คํ์ด ์ ์์ ์ผ๋ก ๋์๊ณ , end ๊น์ง ์ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด์ฒ๋ผ ์ค๋ ๋๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋ ์ญํ ์ ์ ์ํํด์ค๋ค.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void *thread_func(void *arg)
{
char* ret;
int i = *((int*) arg);
int c = *((int*) arg) + 5;
while(i <= c)
{
sleep(1);
printf("running thread %d\n", i);
i ++;
}
ret = (char *)malloc(sizeof(char) * 10);
strcpy(ret, "doyoucode\0");
return (ret);
}
int main(void)
{
pthread_t thread_ID;
void *t_return;
int arg = 0;
if(pthread_create(&thread_ID, NULL, thread_func, (void *)&arg) != 0)
exit(1);
pthread_join(thread_ID, &t_return);
printf("end, return value : %s\n", (char *)t_return);
free(t_return);
return (0);
}
์ถ๋ ฅ - std_output)

์์ ์ฝ๋๋ ์ค๋ ๋์ ๋ฆฌํด๊ฐ์ ํ์ฉํด๋ณด๊ธฐ ์ํ ์ฝ๋์ด๋ค.
์ฐ์ main์์ t_return์ด๋ผ๋ ๋ฆฌํด๊ฐ์ ๋ฐ์ ๋ณ์๋ฅผ ํ๋ ๋ง๋ค์ด์ฃผ๊ณ , ํด๋น ๋ณ์๋
pthread_join์ ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์ ์๋ฆฌ์ ์ฃผ์๊ฐ์ผ๋ก ์ ๋ฌํด์ค๋ค.
๊ทธ๋ผ ์ด์ ์คํ๋ thread_func์์ char * ๋ณ์๋ก ์์ฑ๋ ๊ฐ์ด ๋ฐํ์ด ๋๊ณ ,
ํด๋น ๋ฐํ์ด pthread_join์ ์ํด t_return์ ์ ์ฅ๋๊ณ ํด๋น ๊ฐ์ printf๋ก ์ถ๋ ฅํ ๋๋ void * ์ธ ๋ณ์๋ฅผ
char * ๋ก type casting ํด์ฃผ์ด ์ถ๋ ฅํด ๋ณผ ์ ์๋ค.
์ด๋ฐ์์ผ๋ก ์ค๋ ๋์ ๋ฐํ๊ฐ์ ์ด์ฉํ ์ ์๋๋ก ํด์ฃผ๋ pthread_join ํจ์์ด๋ค.
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [library] atoi ๊ตฌํํ๊ธฐ (0) | 2025.03.12 |
|---|---|
| [function] pthread_detach ํจ์ ์์๋ณด๊ธฐ (2) | 2024.09.18 |
| [function] pthread_create ํจ์ ์์๋ณด๊ธฐ (5) | 2024.09.07 |
| [utility] get_next_line ๊ตฌํํ๊ธฐ (3) | 2024.08.31 |
| [library] itoa ๊ตฌํํ๊ธฐ (0) | 2024.06.29 |
