개발 환경 |
|
OS |
Ubuntu 18.04.2 |
컴파일러 |
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04) |
void pthrad_cleanup_push(void (*routine) (void *), void* arg);
Thread 종료 시 routine 함수가 실행이 됩니다.
arg는 넘겨지는 인자 값.
cleanup handler 함수는
보통 자원을 반납하거나 mutex lock 혹은 unlock을 사용하기 위해서
호출됩니다.
void pthread_cleanup_pop(int execute);
cleanup handler 함수를 제거합니다.
매개변수가 0이면 실행하지 않고 삭제하며
0이 아니라면 실행한 이후에 삭제됩니다.
claenup handler 함수는 자원을 반납하거나, mutex 등의 잠금 해제등을 위한 용도로 사용됩니다. (mutex 에 대한 포스팅은 후에 기회가 닿는대로 하도록 하죠)
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
void pthrad_cleanup_push(void (*routine) (void *), void* arg);
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
void pthrad_cleanup_push(void (*routine) (void *), void* arg);
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]void pthrad_cleanup_push(void (*routine) (void *), void* arg);
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]void pthrad_cleanup_push(void (*routine) (void *), void* arg);
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
void pthrad_cleanup_push(void (*routine) (void *), void* arg);
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
출처: https://bitsoul.tistory.com/166?category=683199 [Happy Programmer~]
소스코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<stdio.h> #include<pthread.h> #include<stdlib.h> #include<unistd.h> // clean up handler void cleanup(void *arg){ printf("Thread Clean up\n"); free(arg); // resource free } // thread function void *thread_function(void * data){ static int retval = 999; int count = *((int *) data); int i = 0; char *mydata; mydata = (char *) malloc(10000); // pthread_cleanup_push를 걸어 자원을 free 시켜줌 pthread_cleanup_push(cleanup, (void *) mydata); while(1){ // if i == 3 => end thread if(i == count ) { pthread_exit((void *) &retval); } printf("Thread Running ... %d : data %d \n", i, count); i++; sleep(1); } // cleanUp handler를 해제시켜준다. pthread_cleanup_pop(0); } int main(){ pthread_t p_thread; int thr_id; void *tret = NULL; int count = 3; thr_id = pthread_create(&p_thread, NULL, thread_function, (void *) &count); if(thr_id < 0) { perror("thread create error\n"); exit(0); } // pthread_join의 두번째 인자 tret에서 받아서 // thread가 끝날 때 return 받는다 pthread_join(p_thread, &tret); // pthread의 반납값 출력 printf("thread exit code %d\n", *((int *) tret)); return 0; } | cs |
실행결과
1 2 3 4 5 6 | $ ./pthread_cleanup Thread Running ... 0 : data 3 Thread Running ... 1 : data 3 Thread Running ... 2 : data 3 Thread Clean up thread exit code 999 | cs |
※ 본 글은 개인 포트폴리오 혹은 공부용으로 사용하기 때문에, 무단 복사 유포는 금지하지만, 개인 공부 용도로는 얼마든지 사용하셔도 좋습니다
'C_C++ 프로그래밍' 카테고리의 다른 글
pthread_mutex_init (뮤텍스 사용하기!) (0) | 2019.05.25 |
---|---|
pthread_attr 사용 (pthread_detach 없이 자원 반납하기) (0) | 2019.05.25 |
thread_exit(쓰레드에서 특정 값을 return을 시키자) (0) | 2019.05.25 |
pthread_detach(쓰레드 자원들을 분리시키자) (0) | 2019.05.25 |
댓글