【发布时间】:2014-06-03 21:13:06
【问题描述】:
我有一个捕获程序,它除了捕获数据并将其写入文件还打印一些统计数据。打印统计数据的函数
static void* report(void)
{
/*Print statistics*/
}
使用每秒到期的 ALARM 大约每秒调用一次。所以程序就像
void capture_program()
{
pthread_t report_thread;
while (!exit_now )
{
if (pthread_create(&report_thread,NULL,report,NULL)) {
fprintf(stderr,"Error creating reporting thread! \n");
}
/*
Capturing code
--------------
--------------
*/
if(doreport)
usleep(5);
}
}
void *report(void *param)
{
while (true)
{
if (doreport)
{
doreport = 0
//access some register from hardware
usleep(5)
}
}
}
计时器到期设置doreport 标志。如果设置了此标志,则调用report() 清除标志。我正在使用usleep 在程序中的两个线程之间交替。这似乎工作正常。
我还有一个信号处理程序来处理 SIGINT(即 CTRL+C)
static void
anysig(int sig)
{
if (sig != SIGINT)
dagutil_set_signal_handler(SIG_DFL);
/* Tell the main loop to exit */
exit_now = 1;
return;
}
我的问题:
1) Is it safe to call pthread_join from inside the signal handler?
2) Should I use exit_now flag for the report thread as well?
【问题讨论】:
标签: c multithreading pthreads posix