pthread_cancel 에 대한 질문입니다..

psjcap의 이미지

pthread_cancel을 호출하게 되면..

cancel하기로 한 thread가 완전히 cancel된 후에 리턴 되나요..
아니면 바로 리턴 되고 thread는 따로 종료되나요..??

그리고 pthread_exit 호출 하면 thread가 완전히 종료되나요..??
만약 pthread_exit 수행 중에 외부에서 pthread_cancel을 호출하면 어떻게 되나요..??

이런거 고민해 보신 분 계신가요..??

dhjung의 이미지

psjcap wrote:
pthread_cancel을 호출하게 되면..

cancel하기로 한 thread가 완전히 cancel된 후에 리턴 되나요..
아니면 바로 리턴 되고 thread는 따로 종료되나요..??

Redhat 9에 있는 man page에 다음과 같이 나와있군요..

pthread_cancel()의 맨페이지 wrote:
When a thread eventually honors a cancellation request, it performs as if pthread_exit(PTHREAD_CANCELED) has been called at that point: all cleanup handlers are executed in reverse order, finalization functions
for thread-specific data are called, and finally the thread stops executing with the return value PTHREAD_CANCELED

psjcap wrote:
그리고 pthread_exit 호출 하면 thread가 완전히 종료되나요..??

"완전히"의 의미가 어떤것이신지??

psjcap wrote:
만약 pthread_exit 수행 중에 외부에서 pthread_cancel을 호출하면 어떻게 되나요..??

역시 맨페이지를 인용하면..

pthread_cancel()의 맨페이지 wrote:
PTHREAD_CANCEL_DEFERRED to keep the cancellation request pending until the next cancellation point
......
Threads are always created by pthread_create(3) with cancellation enabled and deferred. That is, the initial cancellation state is PTHREAD_CANCEL_ENABLE and the initial type is PTHREAD_CANCEL_DEFERRED.

Cancellation points are those points in the program execution where a test for pending cancellation requests is performed and cancellation is executed if positive. The following POSIX threads functions are cancellation points:

pthread_join(3)
pthread_cond_wait(3)
pthread_cond_timedwait(3)
pthread_testcancel(3)
sem_wait(3)
sigwait(3)

All other POSIX threads functions are guaranteed not to be cancellation points. That is, they never perform cancellation in deferred cancellation mode.

적어도 PTHREAD_CANCLE_DEFERRED에서의 pthread_exit()의 경우, system-defined cancellation point에 속하지 않으므로 pthread_cancel()영향을 받지 않을듯 싶네요..

물론, cancellation mode로 PTHREAD_CANCEL_ASYNCHRONOUS 도 있지만, 이 모드의 사용은 위험하고 저도 써보지 않아서 어찌될지는 모르겠네요.. :oops:

PS. Solaris 8의 경우, system-defined cancellation point로 몇몇 시스템콜도 가능한데, 제 Redhat 9에서는 아직 구현안되어있다고 나오네요..


--------------------------
Donghyun Jung

psjcap의 이미지

위 답변글 잘 읽었습니다..
전 일단 솔라리스인 관계로.. 리눅스란 조금 다를 수도 있겠네요..

우선 위 man page로 봐서 대략 결론은..
1. pthread_cancel 호출하면 종료시키려는 thread가 종료된 후 리턴된다..
2. pthread_cancel함수와 pthread_exit 호출로 인해서 호출 되는 함수들 ( 예를 들면 pthread_cleanup_push나 thread specific data에 key 생성시 destructor로 등록된 함수..)에 간섭을 받지 않는다..

요게 맞나요..??

dhjung의 이미지

질문을 다시 읽어보니 제가 아래 인용부분을 잘못 이해하고 글을 적은듯하네여.. :?

dhjung wrote:
psjcap wrote:
pthread_cancel을 호출하게 되면..

cancel하기로 한 thread가 완전히 cancel된 후에 리턴 되나요..
아니면 바로 리턴 되고 thread는 따로 종료되나요..??

Redhat 9에 있는 man page에 다음과 같이 나와있군요..

pthread_cancel()의 맨페이지 wrote:
When a thread eventually honors a cancellation request, it performs as if pthread_exit(PTHREAD_CANCELED) has been called at that point: all cleanup handlers are executed in reverse order, finalization functions
for thread-specific data are called, and finally the thread stops executing with the return value PTHREAD_CANCELED

원질문은 calling thread가 어떻게 되는지를 묻는거였는데, 전 target thread가 어떻게 동작하는지를 묻는줄알고 그에 대한 것을 적었네여.. 죄송.. :cry:

다시 원질문에 대한 대답을 드리면.... pthread_cancel()을 call한 thread는 target thread의 cancel 여부와 상관없이 바로 리턴됩니다.. 즉, asynchronous로 동작합니다..

psjcap wrote:
1. pthread_cancel 호출하면 종료시키려는 thread가 종료된 후 리턴된다..
2. pthread_cancel함수와 pthread_exit 호출로 인해서 호출 되는 함수들 ( 예를 들면 pthread_cleanup_push나 thread specific data에 key 생성시 destructor로 등록된 함수..)에 간섭을 받지 않는다..

1번에 대한 대답은 제가 원질문을 잘못이해해서 글을 적었으니, 바로 윗부분을 다시 한번 봐주세여.. (혼동드려서 죄송... :( )

2번은, "pthread_exit() call에 의해서 호출되어진 함수들(예로 적어주신..)은 system-defined cancellation point라 할 지라도, 재차 호출되는 pthread_cancel()의 영향을 받지 않는다"는 것을 의미하시는 건가요?? (pthread_cancel()은 pthread_exit(PTHREAD_CANCEL)과 같다고 했으므로..)
만약, 제가 이해한 것이 맞다면, 네 그렇습니다... 예전에 제가 처음 cancellation을 사용할 때, solaris에서 테스트해본 기억으로는.. system-defined cancellation point라 할지라도 cleanup 함수이면, cancel request를 무시하는 듯 하더군요..


--------------------------
Donghyun Jung