24

Till recently, I was under the impression that if you "detach" a thread after spawning it, the thread lives even after the "main" thread terminates.

But a little experiment (listed below) goes contrary to my belief. I expected the detached thread to keep printing "Speaking from the detached thread" even after main terminated, but this does not seem to be happening. The application apparently terminates...

Do the "detached" threads die after "main" issues return 0?

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

void *func(void *data)
{
    while (1)
    {
        printf("Speaking from the detached thread...\n");
        sleep(5);
    }
    pthread_exit(NULL);
}

int main()
{
    pthread_t handle;
    if (!pthread_create(&handle, NULL, func, NULL))
    {
        printf("Thread create successfully !!!\n");
        if ( ! pthread_detach(handle) )
            printf("Thread detached successfully !!!\n");
    }

    sleep(5);
    printf("Main thread dying...\n");
    return 0;
}
puffadder
  • 1,814
  • 3
  • 20
  • 32
  • 5
    It is not a good idea to exit the main thread while other threads are running; in any case, returning from main() will cause other threads to be killed anyway. – MarkR May 18 '11 at 11:52

5 Answers5

46

To quote the Linux Programmer's Manual:

The detached attribute merely determines the behavior of the system when the thread terminates; it does not prevent the thread from being terminated if the process terminates using exit(3) (or equivalently, if the main thread returns).

Also from the Linux Programmer's Manual:

To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 3
    Will the process terminate if main() says return 0 (or equivalently) pthread_exit(NULL)? It is just another thread of the process... – puffadder May 18 '11 at 10:15
  • 5
    @puffadder: (1) the main thread isn't "just another thread": there are quite a few cases where pthreads treat the main thread differently from other threads; (2) `return 0` and `pthread_exit()` are not equivalent; I've expanded my answer. – NPE May 18 '11 at 10:33
  • Aha! There you go... I once read somewhere that pthread_exit() and return are same for threads... and that's why all the confusion !!! Thanks a lot for the clarification. – puffadder May 18 '11 at 10:40
  • 6
    The `pthread_exit` function and returning from the starting function are the same for all threads. However, for the thread that's running when the process starts, `main` is *not* its starting function. (How could it be? If it was, what would initialize global variables? What would fill out `argc` and `argv`? What would pass on the value returned from `main`?) When it returns from main, it returns to the function that called `main`, which then goes on to terminate the process. – David Schwartz Sep 17 '11 at 07:29
25

pthread_detach just means that you are never going to join with the thread again. This allows the pthread library to know whether it can immediately dispose of the thread resources once the thread exits (the detached case) or whether it must keep them around because you may later call pthread_join on the thread.

Once main returns (or exits) the OS will reap all your threads and destroy your process.

idz
  • 12,825
  • 1
  • 29
  • 40
6

pthread_detach does not do what you think it does - it indicates to the implementation that the space the thread with the specified ID is using can be reclaimed as soon as it terminates, ie. no pthread_join operation will be performed on it.

All threads are terminated once the process containing them is terminated.

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
3

Yes, the detached threads will die after return 0.

From the NOTES section of man pthread_detach

The detached attribute merely determines the behavior of the system when the thread terminates; it does not prevent the thread from being terminated if the process terminates using exit(3) (or equiv‐alently, if the main thread returns)

freethinker
  • 1,286
  • 1
  • 10
  • 17
1

From man pthread_detach:

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.

Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
Dinesh
  • 11
  • 1