我正在构建一个用C-UNIX编写的通用程序(使用Linux,所以我不在乎BSD或WIN函数),它创建了两个线程来处理与服务器的通信。调用SIGINT时终止线程 - C
void init_threads(int socket_desc) {
pthread_t chat_threads[2];
ret = pthread_create(&chat_threads[0], NULL, receiveMessage, (void*)(long)socket_desc);
PTHREAD_ERROR_HELPER(ret, "Errore creazione thread ricezione messaggi");
ret = pthread_create(&chat_threads[1], NULL, sendMessage, (void*)(long)socket_desc);
PTHREAD_ERROR_HELPER(ret, "Errore creazione thread invio messaggi");
}
由于这一计划将在shell启动我要实现CTRL-C可能性,所以做我这行代码:
signal(SIGINT,kill_handler);
// and its related function
void kill_handler() {
// retrive threads_id
// call pthread_exit on the two threads
printf("Exit from program cause ctrl-c, bye bye\n");
exit(EXIT_SUCCESS);
}
我的问题是如何发现的事件处理函数中的线程ID,调用pthread_exit还是应该使用别的东西?
可能重复[如何正确终止信号处理程序中的线程?](https://stackoverflow.com/questions/25950621/how-to-properly-terminate-a-thread-in-a-signal-处理器) – Gaurav
信号被传递给主线程;退出()在那里,操作系统会为你做清理。 – Ctx
我发誓我真的没有找到20分钟深搜索后的重复。因为在我的计划被誉为费利克斯Palmen比@Ctx的意见更好的答案我使用的插座,如果有开他们的连接插座将保持开放,即使threre是没有听一小段时间。无论如何,感谢大家。 – Thecave3