UNIX Socket FAQ

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

useless79
Member
Registered: 2011-10-10
Posts: 32

per thread signal handling in pthread

Hi ALL,

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.


Thanks...

Offline

#2 2019-02-08 03:03 PM

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

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...

Offline

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

Board footer

Powered by FluxBB