puffadder
puffadder

Reputation: 1824

pthread_detach question

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;
}

Upvotes: 24

Views: 54540

Answers (5)

Dinesh
Dinesh

Reputation: 11

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.

Upvotes: 1

NPE
NPE

Reputation: 500903

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).

Upvotes: 46

Michael F
Michael F

Reputation: 40879

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.

Upvotes: 7

freethinker
freethinker

Reputation: 1306

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)

Upvotes: 3

idz
idz

Reputation: 12998

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.

Upvotes: 26

Related Questions