why multiple SIGINT raises when i hit C-c

hi,

in my application, i have set up to capture SIGINT and execute a handler.the problem is whenever i hit C-c, multiple SIGINT are sent to the application.I have blocked the SIGINT right after catching the first one but it is unsuccessful.Here is what i do :

jmp_buf main_loop;

int main()
{

signal\(SIGINT,CleanExit\);

sigset_t set;
sigset_t old_set;

sigemptyset\(&set\);
sigemptyset\(&old_set\);

sigaddset \(&set, SIGINT\);

// run other threads  ...

while\( 1 \)
\{
    if \( setjmp \(main_loop\) \)
    \{
        pthread\_sigmask\(SIG\_BLOCK,&set,&old_set\);
        /// Clean up and shut down threads 
        pthread\_sigmask\(SIG\_UNBLOCK,&set,&old_set\);
        break;
    \}
\}

printf("\nbye!\n");
return 0;

}

void CleanExit()
{
shutdownThreads = 1;
longjmp (main_loop, 1);
}

The problem is the CleanExit method runs 3 to 4 times which means the pthread_sigmask(SIG_BLOCK,&set,&old_set); has not done the job.

So what is the problem?

My application is running in mulinux.

Thanks in advance,
Sedighzade

Humm, the generally accepted way to handle a signal such as Ctrl-C in a threaded application is to create a specific signal handling thread which waits for a Ctrl-C and then sets a mutex or condition variable which the other threads check on a regular basis.

See Section 6.6 of Programming with POSIX Threads by Dave Butenhof. He provides example code for exactly what you are looking to do on starting on page 228. You can also find other examples if you do a web search.

By the way, it is bad practice to use setjmp and longjmp in threaded applications. Some versions of threads libraries actually use setjmp/longjmp internally for various purposes.