pthread류 함수 중 pop과 push함수 질문입니다.
글쓴이: cmjung88 / 작성시간: 일, 2013/01/20 - 1:03오후
안녕하세요, 시스템 프로그래밍을 APUE책으로 공부하고 있는 학생입니다.
쓰레드 관련 11장 파트를 배우는데 pthread_clean_push함수와 짝이 되는 pthread_clean_pop 함수가 이해가 되지 않아서 질문 올립니다.
제가 알고 있는 pthread_clean_pop함수는 인자가 0일시 쓰레드가 어떤 이유에서 종료되어도, push에서 설정한 함수를 실행하지 않는다! (Non Zero = 실행, Zero 실행X)
인데요..! 아래에는 APUE 책에 있는 예제 소스 코드입니다.
#include "apue.h" #include <pthread.h> void cleanup(void *arg) { printf("cleanup: %s\n", (char *)arg); } void * thr_fn1(void *arg) { printf("thread 1 start\n"); pthread_cleanup_push(cleanup, "thread 1 first handler"); pthread_cleanup_push(cleanup, "thread 1 second handler"); printf("thread 1 push complete\n"); if (arg) return((void *)1); pthread_cleanup_pop(0); pthread_cleanup_pop(0); return((void *)1); } void * thr_fn2(void *arg) { printf("thread 2 start\n"); pthread_cleanup_push(cleanup, "thread 2 first handler"); pthread_cleanup_push(cleanup, "thread 2 second handler"); printf("thread 2 push complete\n"); if (arg) pthread_exit((void *)2); pthread_cleanup_pop(0); pthread_cleanup_pop(0); pthread_exit((void *)2); } int main(void) { int err; pthread_t tid1, tid2; void *tret; err = pthread_create(&tid1, NULL, thr_fn1, (void *)1); if (err != 0) err_quit("can't create thread 1: %s\n", strerror(err)); err = pthread_create(&tid2, NULL, thr_fn2, (void *)1); if (err != 0) err_quit("can't create thread 2: %s\n", strerror(err)); err = pthread_join(tid1, &tret); if (err != 0) err_quit("can't join with thread 1: %s\n", strerror(err)); printf("thread 1 exit code %d\n", (int)tret); err = pthread_join(tid2, &tret); if (err != 0) err_quit("can't join with thread 2: %s\n", strerror(err)); printf("thread 2 exit code %d\n", (int)tret); exit(0); }
그러나 제 예상과는 달리, 두 쓰레드 모두 0을 pop의 인자로 줬음에도 불구하고 thr_fn2의 경우에는 push에서 설정된 함수가 실행되는것을 볼 수 있는데요 (pthread_exit함수를 호출하므로)
왜 pop을 0으로 인자로 줬음에도 불구하고 cleanup:thread 2 second handler, cleanup : thread2 first handler 라는 메시지가 출력(push설정 함수)되는걸까요?
위 예제는 Figure 11.5 in APUE 2E입니다.
참고 링크 : pthread push/ pop함수 설명 http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_cleanup_push.3.html
Forums:
해결하였습니다.
해외 포럼에 올려서 답을 구하였습니다.
http://stackoverflow.com/questions/14421688/pthread-cleanup-pop-with-argument-0 참고
댓글 달기