eduardosufan
eduardosufan

Reputation: 1582

How to wait a signal in a thread?

I'm sending a signal from a module in the kernel space to a process. This process has one thread waiting for the signal.

I read the signal manual and it says: The signal disposition is a per-process attribute: in a multithreaded application, including the disposition of a signal is the same for all threads.

Thus, and according to the manual pthread_sigmask: http://man7.org/linux/man-pages/man3/pthread_sigmask.3.html

I'm trying to block the signal in the main function of the application by calling:

siginfo_t infoh1;
sigset_t waith1;

sigemptyset(&waith1);
sigaddset(&waith1, SIG_HILO1);
pthread_sigmask( SIG_BLOCK, &waith1, NULL );

Note that the thread is waiting for it in its execution function.

result = sigwaitinfo (&waith1, &infoh1);

The signal is sent, but the thread never receives it (it hangs waiting).

What am I doing wrong? I tested with different codes from different websites without success.

Upvotes: 0

Views: 1640

Answers (1)

Oakdale
Oakdale

Reputation: 138

I use signals a lot in my *nix code and I don't think this is a good approach.

I recommend that all threads are set to ignore signals. The main process handles the signal while the thread sits on a mutex/condition. On signal the main process sets a global static flag with the signal type and then notifies the thread which duly checks the flag to see which signal was caught.

It's a safe and easy solution.

Upvotes: 2

Related Questions