Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    threads & signals

    Hi, Im writing a program that has a few threads, each of one eventually blocks waiting for a message to be sent into a message queue (each thread waits on its own queue). I wanted to implement some sort of timeout in order not to block the thread permanetly if the message is never sent to the queue. I thought programming an alarm with alarm() but Im not sure which thread will receive the SIGALARM. According to what I read, each thread should set its own handle of the SIGALARM signal and the signal will be sent to the thread that set the alarm(). is this correct? is there any chance that signals are sent to another thread? if not, what happens if the thread finished before the alarm is the signal handler executed anyway?
    btw, i set signal handlers with signal() function
    well, thank you all

  2. #2
    Each thread has set its own signal mask via pthread_sigmask(). Right?

    However signal disposition is shared by all threads in the process.
    Call sigwait in any thread, but it will not block if there are pending signals.

    Try using setitimer() with ITIMER_REAL which will send SIGALRM.
    Do not use ITIMER_VIRTUAL.

    Block that signal except in threads you want to force into automatically commiting suicide.

    Signals are painful to implement smoothly, even more painful in threaded processes. Be careful.

  3. #3


    as far as i know, signal() set a signal handler for process, not for thread.

    int sigwait(const sigset_t *set, int *sig);

    sigwait suspends the calling thread until one of the signals in set is
    delivered to the calling thread. It then stores the number of the sig-
    nal received in the location pointed to by sig and returns. The sig-
    nals in set must be blocked and not ignored on entrance to sigwait.
    If the delivered signal has a signal handler function attached, that
    function is not called.

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts