- 论坛徽章:
- 0
|
本帖最后由 rocklinux 于 2012-03-07 11:15 编辑
回复 2# timespace
1) 这是第一种方案,main thread注册signal handler处理信号
sigset_t newmask, saved_mask;
sigfillset(&newmask);
sigdelset(&newmas, SIGINT);
if (pthread_sigmask(SIG_SETMASK, &newmask, &saved_mask) < 0)
return ERR_FAIL;
struct sigaction act, saved_act;
act.sa_handler = s_sig_handler_func;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(SIGINT, &act, &saved_act) < 0)
return ERR_FAIL;
2)这是第二种方案,开启一个独立线程,专门处理signal
// in main thread
ret = pthread_create(&m_sig_handler_thread, NULL, s_sig_handler_thread_func, NULL);
// a dedicated thread
void *s_sig_handler_thread_func(void *)
{
int rc = 0;
sigset_t waitset;
sigemptyset(&waitset);
sigaddset(&waitset, SIGINT);
while (1) {
rc = sigwaitinfo(&waitset, NULL);
if (rc != SIGINT)
continue;
cout << "Signal handler thread " << pthread_self() << " received a SIGINT." << endl;
break;
}
return NULL;
}
|
|