사용자가 인터럽트 (예 : CTRL + C)를 보내고 output_report() 메서드가 실행될 때까지 영원히 실행되는 멀티 스레드 응용 프로그램을 만듭니다. 다음 코드 샘플은 다음과 같습니다pthread 조건이 만족스럽지 않다
void output_report(int signo) {
printf("Exiting!\n");
pthread_mutex_lock(&mutex_num_of_threads);
programClosing = true;
while (numOfThreads != 0){
pthread_cond_wait(&allThreadsCompleteCond, &mutex_num_of_threads);
}
pthread_mutex_unlock(&mutex_num_of_threads);
printf("Closing Now!\n"); //This part is not reached
pthread_exit(NULL); // Is this needed?
exit(0);
}
void dispatch(struct pcap_pkthdr *header, const unsigned char *packet,
int verbose) {
static bool thread_settings_initialised = false;
//Only run the first time dispatch method runs
if (thread_settings_initialised == false){
thread_settings_initialised = true;
if (signal(SIGINT, output_report) == SIG_ERR)
fprintf(stderr, "\ncan't catch SIGINT\n");
//...
//Set mutex for the appropriate variables to remain thread safe
pthread_mutex_init(&mutex_num_of_threads, NULL);
//...
//Set attr so threads are "Detached"
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
//Set pthread_cond_init
pthread_cond_init(&allThreadsCompleteCond, NULL);
}
//...
pthread_mutex_lock(&mutex_num_of_threads);
numOfThreads++;
pthread_mutex_unlock(&mutex_num_of_threads);
//...
int rc = pthread_create(&tid, &attr, analyse, (void *) &data);
//...
}
void analyse(void *thread_data) {
//...
pthread_mutex_lock(&mutex_num_of_threads);
numOfThreads--;
if (programClosing == true && numOfThreads == 0) {
pthread_cond_signal(&allThreadsCompleteCond);
}
pthread_mutex_unlock(&mutex_num_of_threads);
pthread_exit(NULL);
}
내 문제는 내가 CTRL + C를 사용하여 어느 때, 프로그램은 단지 종류의 프로그램이 계속 실행되고 내가 Ctrl 키를 사용해야합니다 (완전하게 종료를 중단한다는 것입니다. + z를 눌러 빠져 나옵니다.) 프로그램은 "Exiting"을 출력하지만 "지금 닫기"는 "allThreadsCompleteCond"가 충족되지 않는다는 의미는 아니지만 그 이유는 모르겠습니다.
UPDATE
덕분에 폴 그리피스의 대답에 나는 그렇게처럼 내 코드를 업데이트 :
void exitHandler(int signum){
programClosing = 1;
}
void output_report(int signo) {
while (programClosing == 1){
printf("Exiting!\n");
//rest same as before
exit(0)
}
}
void dispatch(struct pcap_pkthdr *header, const unsigned char *packet,
int verbose) {
static bool thread_settings_initialised = false;
int rc;
printf("DISPATCH!\n");
//Only run the first time dispatch method runs
if (thread_settings_initialised == false){
thread_settings_initialised = true;
//Set mutex for the appropriate variables to remain thread safe
//..
//Set attr so threads are "Detached"
//..
//...
if (signal(SIGINT, exitHandler) == SIG_ERR)
fprintf(stderr, "\ncan't catch SIGINT\n");
pthread_t exit_tid;
rc = pthread_create(&exit_tid, &attr, output_report, (void *) NULL);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
//...
///same as before
}
이제 업데이트 된 코드는 여전히 않아도 출력되지 텍스트 "종료"!
'의 printf()를''에 pthread_mutex_lock()''는 pthread_cond_wait() '및'pthread_mutex_unlock을() '신호 처리기에서 호출하는 것이 안전하지 않다. 당신이 보는 행동은 그 증거입니다. 전략을 재고해야합니다. –
대체 뭐하는거야 –
아마도 신호 처리기가 아무것도하지 않고'sig_atomic_t' 유형의 상태 변수를 설정하고 주기적으로 단일 스레드에서이를 확인하고 다른 스레드가 변경된 경우이를 닫습니다. 일반적으로 스레드와 신호를 혼합하면 이와 같은 많은 문제가 발생합니다. –