pthread_join from a signal handler
        Posted  
        
            by 
                liv2hak
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by liv2hak
        
        
        
        Published on 2014-06-03T21:13:06Z
        Indexed on 
            2014/06/03
            21:24 UTC
        
        
        Read the original article
        Hit count: 267
        
I have a capture program which in addition do capturing data and writing it into a file also prints some statistics.The function that prints the statistics
static void* report(void)
{
         /*Print statistics*/
}
is called roughly every second using an ALARM that expires every second.So The program is like
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)
            }
       }
}
The expiry of the timer sets the doreport flag.If this flag is set report() is called which clears the flag.I am using usleep to alternate between two threads in the program.This seems to work fine.
I also have a signal handler to handle SIGINT (i.e 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;
}
My question:
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?
© Stack Overflow or respective owner