Do You Coding?

[function] pthread_detach ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[function] pthread_detach ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“Œ ๋งค๋‰ด์–ผ (Linux)

๋”๋ณด๊ธฐ

NAME
       pthread_detach - detach a thread

SYNOPSIS
       #include <pthread.h>
       int pthread_detach(pthread_t thread);
       Compile and link with -pthread.

DESCRIPTION
       The  pthread_detach() function marks the thread identified by thread as detached.

       When a detached thread terminates, its resources are automatically released back to the system

       without the need for  another  thread  to join with the terminated thread.
       Attempting to detach an already detached thread results in unspecified behavior.

RETURN VALUE
       On success, pthread_detach() returns 0; on error, it returns an error number.

ERRORS
       EINVAL thread is not a joinable thread.
       ESRCH  No thread with the ID thread could be found.

 

๐Ÿ“Œ ํ•จ์ˆ˜ ์„ค๋ช…

์ด์ „์— pthread ํ•จ์ˆ˜๋“ค์„ ํ†ตํ•ด ์Šค๋ ˆ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ํ–ˆ๋‹ค๋ฉด, ์ด๋ฒˆ์—๋Š” ๋ถ„๋ฆฌํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

pthread_detach ํ•จ์ˆ˜๋Š” ๋ง๊ทธ๋Œ€๋กœ ํŠน์ • ์Šค๋ ˆ๋“œ๋ฅผ ๋ถ„๋ฆฌ(detach) ์ƒํƒœ๋กœ ์„ค์ •ํ•˜์—ฌ ํ•ด๋‹น ์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ์ž๋™์œผ๋กœ

๊ทธ ์ž์›์„ ํ•ด์ œํ•˜๋„๋ก ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ฉ”์ธ ์Šค๋ ˆ๋“œ๋‚˜ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ํ•ด๋‹น ์Šค๋ ˆ๋“œ์˜ ์ข…๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ ๋„

์ž์› ๋ˆ„์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

int pthread_detach(pthread_t thread);

๊ธฐ๋ณธํ˜•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ถ„๋ฆฌํ•˜๊ณ ์ž ํ•˜๋Š” ์Šค๋ ˆ๋“œ์˜ ์‹๋ณ„์ž์ด๊ณ , ๋ฐ˜ํ™˜ ๊ฐ’์€ int ํ˜•์œผ๋กœ ์˜ค๋ฅ˜ ์ฝ”๋“œ(errno)์ด๋‹ค.


#include <pthread.h>
#include <stdio.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() {
    pthread_t thread;
    int result;
    int arg = 0;

    result = pthread_create(&thread, NULL, thread_func, (void *)&arg);
    if (result != 0) {
        perror("pthread_create ์‹คํŒจ");
        return -1;
    }
    result = pthread_detach(thread);
    if (result != 0) {
        perror("pthread_detach ์‹คํŒจ");
        return -1;
    }
    printf("main running...\n");
    pthread_exit(NULL);
}

 

์ถœ๋ ฅ - std_output)

 

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. ์ €๋ฒˆ ๊ฒŒ์‹œ๊ธ€์—์„œ ์“ฐ์ธ thread_func๋ฅผ ๋˜‘๊ฐ™์ด create ํ•ด์คฌ๊ณ ,

ํ•ด๋‹น createํ•œ ์Šค๋ ˆ๋“œ๋ฅผ pthread_detach๋กœ ๋ถ„๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์ด์ œ ๋งŒ๋“ค์–ด์ง„ ์Šค๋ ˆ๋“œ๋Š” main ์Šค๋ ˆ๋“œ์™€ ๋ถ„๋ฆฌ๋œ ์ƒํƒœ๊ฐ€ ๋˜์–ด,

์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ž๋™์œผ๋กœ ์ž์›์ด ํ•ด์ œ ๋˜๋ฏ€๋กœ pthread_join์„ ํ˜ธ์ถœํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

๋งŒ์•ฝ, ์œ„ ์ƒํ™ฉ์—์„œ pthread_detach()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒ๋˜๋”๋ผ๋„ ์ž์›์ด ํ•ด์ œ๋˜์ง€ ์•Š๋Š”๋‹ค.

๋ฐ˜๋“œ์‹œ pthread_join()์„ ํ˜ธ์ถœํ•˜์—ฌ ์Šค๋ ˆ๋“œ๊ฐ€ ์‚ฌ์šฉํ•œ ์ž์›์„ ํ•ด์ œํ•ด์•ผ ํ•˜๋ฉฐ,

pthread_join()์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

(์ฝ”๋“œ์—์„œ pthread_exit(NULL)์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์ด๋Š” ํŠน์ • ์Šค๋ ˆ๋“œ๋งŒ ์ข…๋ฃŒ์‹œํ‚ค๊ณ  ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋“ค์€ ๊ณ„์†

์‹คํ–‰ํ•˜๊ณ ์žํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋ณดํ†ต ๋ฉ”์ธ ํ•จ์ˆ˜์—์„œ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋“ค์ด ์ž‘์—…์„ ์™„๋ฃŒํ•  ์‹œ๊ฐ„์„ ์ฃผ๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.)

 

pthread_join ๊ณผ pthread_detach ๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒํ•˜๋ฉด ์ž์›์„ freeํ•ด์ฃผ๋Š”๋ฐ,

pthread_join์€ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ฐ›์•„์™€ ๋˜ ๋‹ค๋ฅธ ์ œ์–ด๋ฅผ ํ•œ๋‹ค๋ฉด,

pthread_detach๋Š” ์ œ์–ด ์—†์ด ๋ฐ”๋กœ ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ freeํ•ด์ค€๋‹ค.