Will Threads Continue to Run if a Process Exits

7 Answers 7

If you detach the thread, the process will not actually end until the last detached thread has finished, however only the detached threads will run.

You can detach a thread using pthread_detach.

For this to work though, you have to exit the main thread (the one running the main function) using pthread_exit and not exit or by returning from it.

answered Sep 12, 2013 at 6:28

Some programmer dude's user avatar

3

  • I would like to down-vote this, but I do not have sufficient reputation. Detaching thread using pthread_detach() does not cause it to continue running after the main() exits.

    Sep 12, 2013 at 8:31

  • @user1969104 Added note that pthread_exit has to be used, and not exit or return.

    Sep 12, 2013 at 9:25

  • pthread_exit is sufficient and pthread_detach is not required.

    Sep 12, 2013 at 9:29

It is not possible. Process consists of threads and thread cannot exist by itself.

However, if you meant 'main thread has exited' instead of 'main process has exited', then see the last explanation below in case where main() exits without calling pthread_exit. In this case all the threads are terminated implicitly. If main() called pthread_exit before termination, then only the main thread exits and the other created threads continue to run.

There are several ways in which a thread may be terminated:

  • The thread returns normally from its starting routine. It's work is done.
  • The thread makes a call to the pthread_exit subroutine - whether its work is done or not.
  • The thread is canceled by another thread via the pthread_cancel routine.
  • The entire process is terminated due to making a call to either the exec() or exit()
  • If main() finishes first, without calling pthread_exit explicitly itself

answered Sep 12, 2013 at 6:26

user1969104's user avatar

It is not possible to have an alive thread after main process has exited. However using pthread_exit(..) instead of exit(..) inside main(..), you can wait for other threads to exit. This will terminate the main thread but other threads will continue to execute.

For more information about pthread_exit(), visit this link.

answered Sep 12, 2013 at 6:43

Don't You Worry Child's user avatar

2

  • @alk : Thanks for pointing that out. I modified my answer. Hope it resolves the conflict. :-)

    Sep 12, 2013 at 6:50

  • In this case as i think main process will return value specified in pthread_exit(); at the time of actual exit() when all threads pthread_exit(). As well process will not return value to (shell suppose) till last thread does not pthread_exit(), so command prompt will look like hanged process?

    Sep 12, 2013 at 8:31

It is not possible to make thread stay alive even if the main process has exited. As thread are part of main process and it uses the same resource, once main program got exited it will also free its resources. So thread can not be alive after process exit.

answered Oct 16, 2013 at 10:39

Abhitesh khatri's user avatar

A thread runs "under" a process. The main() code runs in a thread. Any other threads that you create in main() (or in other threads) run under the same process.

It should be possible to let your main() thread exit WITHOUT waiting to join other threads that are currently running under the same process. However, I mostly use Windows and have NOT tried this running in a *nix system.

If you do this make sure each thread releases its resources prior to exiting.

answered Sep 12, 2013 at 6:28

JackCColeman's user avatar

What you can do is to not exit main(). At the end of your processing in main, right where you would otherwise exit, just put in something like:

              int main(void) {     ...     ...      while (1) sleep(10);      return (0); }                          

... or something similar. A process is a container for all the threads spawned within it, and it must exist for the threads to continue to exist. There is no harm in leaving the main() thread alive so that other threads continue to execute.

answered Sep 12, 2013 at 6:29

Ziffusion's user avatar

A thread on itself cannot be made running outside its generating main thread. It's the definition of thread.

Now, what you can do is to have that thread spawn another process with all the relevant information and then joining back to the main thread before dying.

Anything else I fear will require the main thread to be kept alive.

answered Sep 12, 2013 at 6:36

EnzoR's user avatar

millsnexce1950.blogspot.com

Source: https://stackoverflow.com/questions/18756836/how-to-keep-a-thread-alive-after-the-main-process-exited

0 Response to "Will Threads Continue to Run if a Process Exits"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel