pthread 잠재우기;;
글쓴이: changkhan / 작성시간: 수, 2003/09/24 - 3:50오후
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
////////////////////////////////
pthread_t thread;
void* Thread(void* pArgs);
////////////////////////////////
void sigInt(int sig)
{
int res = pthread_cancel(thread);
sleep(1);
printf("sdfsdf %d\n", res);
exit(1);
}
void main(int argc, char* argv[])
{
try {
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, sigInt);
pthread_create(&thread, NULL, Thread, NULL);
while(true)
{
printf("Main\n");
usleep(3000);
}
}
catch(...) {
}
}
void* Thread(void* pArgs)
{
while(true)
{
printf("Thread\n");
usleep(3000);
}
}
대충 위와 같은 프로그램이 있다고 치구요;;
프로그램에는 메인루프와 스레드루프가 있습니다.
메인루프와 스레드루프가 돌다가, 프로그램을 종료하고싶어서 sigInt를 호출하거나, Ctrl+c를 누르게 되면, 스레드루프를 강제로 빠져나오게 하고 싶은데, 생각대로 잘 안되네요;;
지금의 소스를 컴파일 해서 테스트를 해 보면, pthread_cancel이 호출 된 뒤에도 스레드루프가 도는것을 확인할 수가 있습니다;;
어떤 함수를 사용하면, 스레드루프를 완전히 죽일 수 있을까요?
단, 스레드 루프 중간에 pthread_exit같은 코드는 왠만하면 쓰고싶지 않습니다.
Forums:


pthread_cancel(3) 의 man page만 자세히 보셔도 아실
pthread_cancel(3) 의 man page만 자세히 보셔도 아실 수 있는 문제입니다.
cancellation state 와 cancellation type , cancellation points 등의 용어를 잘 살펴 보세요.
제가 볼때 pthread_cancel() 을 사용하여 thread를 cancel 시키는 방법 보다는, 전역변수를 변경시키고, 해당 thread는 일정 시간마다(혹은 적절한 시점에) 전역변수를 체크하여 thread 스스로가 죽는 - pthread_exit()으로 - 방법이 더 안전하다고 생각합니다.
우리 모두 리얼리스트가 되자. 그러나 가슴에 이룰 수 없는 꿈을 가지자
댓글 달기