A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » per thread signal handling in pthread

#1 2019-02-08 11:01 AM

Registered: 2011-10-10
Posts: 32

per thread signal handling in pthread


I have written a pthread application having two threads and one main thread. I want to register a SIGINT signal such that when I press ctr+C the programs Thread1 should send "hi" message, Thread2 should send "bye" message and then program should be terminated. I know if I register a signal handler in my main thread, then only one thread will listen that but not all.

In sort I want to register different SIGINIT signal handlers per thread basis so that each thread performs its task before entire program terminates.



#2 2019-02-08 03:03 PM

From: Boston, MA
Registered: 2002-06-12
Posts: 3,839

Re: per thread signal handling in pthread

It can't really be done...  Signals and handlers are per-process, not per-thread...  You might be able to pull something off using a single handler function and something else like pthread_getspecific() to allow for per-thread customized behavior...  But, I think you'll find that when hit with the SIGINT, just one of the two threads, basically at random, will actually handle the signal, not both of them...  You'd be best off just picking one of them to handle it and have the other block it, then that should ensure it's always the one hit with the signal; then, have the handler trigger the other thread's behavior via some other method, like pthread_cond_signal() or pthread_kill() with some other signal like SIGUSR1 or something...


  • Index
  • » Threads
  • » per thread signal handling in pthread

Board footer

Powered by FluxBB