Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux C++: Does a return from main() cause a multithreaded app to terminate?

This question seems like it's probably a duplicate, but I was unable to find one. If I missed a previous question, apologies.

In Java, where I have most of my experience, if your main() forks a thread and immediately returns the process continues to run until all (non-daemon) threads in the process have stopped.

In C++, this appears not to be the case - as soon as the main thread returns the process is terminating with other threads still running. For my current app this is easily solved with the application of pthread_join() but I'm wondering what causes this behavior. Is this compiler (gcc) specific, pthreads specific, or is kind of behavior shared across most/all platforms for which C++ has been implemented? Is this behavior configurable within pthreads (I've looked through the pthread api at the pthread_attr_*() functions and didn't see anything that looked relevant.)?

Completely separate question, but while you're here ... what would one use pthread_detatch() for?

like image 233
Greg Howell Avatar asked Jul 22 '10 02:07

Greg Howell


People also ask

What happens to threads when main exits?

All the threads in your process will be terminated when you return from main() . The libc library is the one responsible for implementing this behavior by calling exit() when the main() function returns.

Does exit terminate all threads?

Calling the exit subroutine terminates the entire process, including all its threads. In a multithreaded program, the exit subroutine should only be used when the entire process needs to be terminated; for example, in the case of an unrecoverable error.

What happens when the main thread stops running in a multi threaded program?

2: It must be the last thread to finish execution. When the main thread stops, your program terminates.


2 Answers

Yes. In modern linux (more importantly newer versions of GNU libc) exit_group is the system call used when main returns, not plain exit. exit_group is described as follows:

This system call is equivalent to exit(2) except that it terminates not only the calling thread, but all threads in the calling process's thread group.

It is worth noting that current the c++ standard makes no mention of threads, so this behavior is not c++ specific, but instead is specific to your particular implementation. That said, every implementation I've personally seen kills all threads when the main thread terminates.

EDIT: It is also worth noting Jonathan Leffler's answer which points out that the POSIX standard does indeed specify this behavior, so it is certainly normal for an application using pthreads for its threading.

EDIT: To answer the follow up about pthread_detach. Basically it is considered a resource leak if you do not join a non-detached thread. If you have a long running task which you have no need to "wait for", and it just "ends when it ends" then you should detach it which will not have a resource leak when it terminates with no join. The man page says the following:

The pthread_detach() function marks the thread identified by thread as detached. When a detached thread terminates, its resources are automatically released back to the system without the need for another thread to join with the terminated thread.

So a quick and dirty answer is: "when you don't care when it ends, detach it. If another thread cares when it ends and must wait for it to terminate, then don't."

like image 86
Evan Teran Avatar answered Nov 15 '22 18:11

Evan Teran


Yes

The POSIX standard says:

§3.297 Process Termination

There are two kinds of process termination:

  1. Normal termination occurs by a return from main(), when requested with the exit(), _exit(), or _Exit() functions; or when the last thread in the process terminates by returning from its start function, by calling the pthread_exit() function, or through cancellation.

  2. Abnormal termination occurs when requested by the abort() function or when some signals are received.

The first normal termination condition applies. (Note that the C++ (1998, 2003) standard says nothing about threads.)


Regarding pthread_detach()

The POSIX standard (again) says:

The pthread_detach() function shall indicate to the implementation that storage for the thread thread can be reclaimed when that thread terminates. If thread has not terminated, pthread_detach() shall not cause it to terminate.

And the rationale says:

The pthread_join() or pthread_detach() functions should eventually be called for every thread that is created so that storage associated with the thread may be reclaimed.

It has been suggested that a "detach" function is not necessary; the detachstate thread creation attribute is sufficient, since a thread need never be dynamically detached. However, need arises in at least two cases:

  1. In a cancellation handler for a pthread_join() it is nearly essential to have a pthread_detach() function in order to detach the thread on which pthread_join() was waiting. Without it, it would be necessary to have the handler do another pthread_join() to attempt to detach the thread, which would both delay the cancellation processing for an unbounded period and introduce a new call to pthread_join(), which might itself need a cancellation handler. A dynamic detach is nearly essential in this case.

  2. In order to detach the "initial thread" (as may be desirable in processes that set up server threads).

like image 39
Jonathan Leffler Avatar answered Nov 15 '22 19:11

Jonathan Leffler