4

How can a program be stopped from a non-main thread?

I'm creating a new thread.

/* Listen for user commands in new thread */
pthread_t tid;
pthread_create(&tid, NULL, &commands, NULL);

I want to stop the program if the user types exit.

/* Listen for user input */
void *commands(void *arg)
{
    char command[100]; 
    while(1)
    {
        printf("Type 'exit' to stop program\n");
        fgets (command, 100, stdin); 
        if(strcmp(command, "exit") == 0) {
            // TODO exit
        }
    }
}

I tried calling exit(0) from the commands method but this did nothing.

Frederik
  • 1,221
  • 2
  • 13
  • 22
  • 3
    `exit(0)` should indeed do something, namely exit the whole process killing all threads. If you just want to exit the thread, just *return* from the function. – Some programmer dude May 28 '21 at 08:33
  • 2
    Ideally you should always end the program gracefully. That means signalling all threads that they should stop, then wait for them to do so. Otherwise you leak resources and might end up with a "ghost process". If any thread should be able to call `exit`, you could implement an `atexit` callback from the main thread who created all others. But ideally only that thread should make the call to close the process. – Lundin May 28 '21 at 08:37
  • @Someprogrammerdude Hm, if exit(0) should do the job then there's probably something else wrong with my program cause the program definitely did not stop. I'll check it out later. – Frederik May 28 '21 at 09:06
  • 1
    You might want to put a `printf` where the `// TODO` comment is, just to make sure that the `if` worked as expected. I don't think it did. See [Remove the newline that `fgets` puts in the buffer](https://stackoverflow.com/a/28462221/3386109). – user3386109 May 28 '21 at 09:14
  • 1
    Yep feeling like a noob now :D The `if` block gets never accessed. It should be `strcmp(command, "exit\n")` and not `strcmp(command, "exit")`. I was sure that I've checked that out before asking the question, but it seems like I have not.... – Frederik May 28 '21 at 09:19
  • 'Ideally you should always end the program gracefully. That means signalling all threads that they should stop, then wait for them to do so. Otherwise you leak resources and might end up with a "ghost process"' premature stoptimization:( – Martin James May 28 '21 at 09:34

2 Answers2

1

You should call pthread_exit to exit your thread correctly. Note that your application should wait for it to exit using pthread_join.

PierreOlivier
  • 1,387
  • 9
  • 23
1

To answer my own question: Exiting the program from a new thread is possible via exit() function. In my code sample the if block which contains the exit() function was never accessed. The following code works fine:

/* Listen for user input */
void *commands(void *arg)
{
    char command[100]; 
    while(1)
    {
        printf("Type 'exit' to stop program\n");
        fgets (command, 100, stdin); 
        if(strcmp(command, "exit\n") == 0) {
            exit(0)
        }
    }
}

However, I'm not sure if just calling exit() stops other threads correctly.

Frederik
  • 1,221
  • 2
  • 13
  • 22