[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

threads && signals problem


	It seems that there is a bug (deviation from standard?) in
OpenBSD threads' library.  Those bugs causes difficulties in writing
programs which use signals.

Let's take following program as an example:

#include <stdio.h>
#include <pthread.h>
#include <signal.h>

void handler (int unused)
    printf ("SIGINT handler\n");
    signal (SIGINT, handler);

void *start (void *arg)
    sigset_t ss;

    sigfillset (&ss);
    sigdelset(&ss, SIGTERM);

    pthread_sigmask (SIG_BLOCK, &ss, NULL);
    poll (NULL, 0, 10000);
    return NULL;

int main()
    pthread_t thr;
    sigset_t ss;

    sigemptyset (&ss);
    sigaddset (&ss, SIGINT);
    pthread_sigmask (SIG_UNBLOCK, &ss, NULL);

    signal (SIGINT, handler);
    pthread_create (&thr, NULL, start, NULL);
    pthread_join (thr, NULL);
    poll (NULL, 0, 1000);
    return 0;

This program won't respond to INT signal till pthread_join exits
(because this function blocks signals and "start thread" does it
explicite).  This is improper behaviour.  I gave this example because
it is common (and also many tutorials advise to so) programming
practice to have separate thread which would service signals.
Is there any elegant "get around" for this problem?

Thank you for your answers.