2

I'm building a generic program written in C-UNIX (using Linux so I don't care about BSD or WIN functions), that creates two threads to handle the communication with a server.

void init_threads(int socket_desc) {

    pthread_t chat_threads[2];

    ret = pthread_create(&chat_threads[0], NULL, receiveMessage, (void*)(long)socket_desc);
    PTHREAD_ERROR_HELPER(ret, "Errore creazione thread ricezione messaggi");

    ret = pthread_create(&chat_threads[1], NULL, sendMessage, (void*)(long)socket_desc);
    PTHREAD_ERROR_HELPER(ret, "Errore creazione thread invio messaggi");

}

Since this program will be launched from shell I want to implement the CTRL-C possibility and so did I with this line of code:

signal(SIGINT,kill_handler);
// and its related function
void kill_handler() {
        // retrive threads_id
        // call pthread_exit on the two threads
        printf("Exit from program cause ctrl-c, bye bye\n");
        exit(EXIT_SUCCESS);
      }

My question is how can I found out the thread ids inside the event handler function and is it correct to call pthread_exit or should I use something else?

Thecave3
  • 762
  • 12
  • 27
  • 2
    Possible duplicate of [How to properly terminate a thread in a signal handler?](https://stackoverflow.com/questions/25950621/how-to-properly-terminate-a-thread-in-a-signal-handler) – Gaurav Pathak Jun 19 '17 at 11:22
  • 1
    The signal is delivered to the main thread; just exit() there, the os will do the cleanup for you. – Ctx Jun 19 '17 at 11:27
  • I swear I really did not found the duplicate after 20 minutes of deep search. Reputed the answer of Felix Palmen better than the comment of @Ctx because in my program I am using sockets and if there's a connection opened on them the sockets will remain open for a short period even if threre's none to listen. Anyway, thanks to everyone. – Thecave3 Jun 19 '17 at 11:53

1 Answers1

2

Don't call pthread_exit() from a signal handler! It is not required to be async-signal-safe, see signal-safety.

In general, you should do as little as possible in a signal handler. The common idiom is to just set a flag that is periodically checked in your main loop like e.g.

volatile sig_atomic_t exitRequested = 0;

void signal_handler(int signum)
{
    exitRequested = 1;
}

int main(void)
{
    // init and setup signals

    while (!exitRequested)
    {
        // do work
    }

    // cleanup
}

Also, use sigaction() for installing signal handlers. See signal() for reasons not to use it.

  • Thanks for the useful links and explaination – Thecave3 Jun 19 '17 at 11:45
  • I know that's unrelated to my question, but could you explain to me what is the sig_atomic_t type and why did you use that instead of an int? I know that integer is 4 byte on 32bit machines, this is tighter? – Thecave3 Jun 19 '17 at 11:52
  • 1
    It's a type that guarantees reads and writes with a *single* instruction. There's a good chance that it's just a `typedef` to `int`, but there might be systems where `int` needs more than one instruction to read/write (e.g. on an 8bit architecture where `int` has 16 bits) -- for a consistent value between signal handler and main program, just always use `volatile sig_atomic_t`. –  Jun 19 '17 at 11:54