About pthread_detach()

I'm new to pthread and confused about the code below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <pthread.h>
#include <iostream>
#include <string.h>


void* thr_fn1(void* arg)
{
        printf("thread 1 returning.\n");
        return((void*)1);
}

void* thr_fn2(void* arg)
{
        printf("thread 2 exiting.\n");
        return((void*)2);
}

int main()
{
        pthread_t tid1, tid2;
        void* tret;
        pthread_create(&tid1, NULL, thr_fn1, NULL);
        pthread_create(&tid2, NULL, thr_fn2, NULL);
        // pthread_join(tid1, &tret);
        pthread_detach(tid1);
        printf("thread 1 exit code % d\n", *((int*)(&tret)));
        //pthread_join(tid2, &tret);
        pthread_detach(tid2);
        printf("thread 2 exit code % d\n", *((int*)(&tret)));
        exit(0);
}

The code is executed in 3 ways:

1. Both use pthread_join(). The output is straightforward. The program won't go on until pthread_join() return.

2. Both use pthread_detach(). The order of sub-thread and main-thread is uncertain. the program won't wait return of fn1 or fn2.

3. Use neither pthread_join() or pthread_detach(). The output seems the same with case 2.

I can understand case 1. However, why the outputs of case 2 and case 3 seem the same? In that case, what is the effect of pthread_detach()?
Maybe the outputs are different(I am not sure) then why are they different?

Thanks in advance.
Last edited on
You do realize that (1) can also produce different outputs?

One possiblity:
thread 1 returning.
thread 2 exiting.
thread 1 exit code  1
thread 2 exit code  2

Another possibility:
thread 1 returning.
thread 1 exit code  1
thread 2 exiting.
thread 2 exit code  2

A third possibility:
thread 2 exiting.
thread 1 returning.
thread 1 exit code  1
thread 2 exit code  2


In (2) and (3) it's possible that "thread 1 returning." and/or "thread 2 exiting." are never printed, if the main thread terminates before those messages are printed.

I think "detach" does not affect how the threads run. It's just that if the thread is detached it will be cleaned up automatically when it terminates. If it's not detached it is not cleaned up completely, so might still use some memory (and possibly other resources?) until you join it.
Last edited on
Actually I don't realize these different outputs.

May you tell me why it hannpens?

Thanks.


Edit: The main confusion is whether the sub-thread begins when pthread_join()/pthread_detach() is called or when pthread_create() is called.
Last edited on
Hello,I think the subthread begins when you call pthread_create().If you don't call pthread_detach(),the subthread begins,and if you call it,the subthread still begins.The difference between case2 and case3 is that if you call pthread_detach().The subthread's resource will be released automatically when it terminates,and if you don't call it,some resources it has can't be released.
Yeah, the new thread starts as soon as you call pthread_create.
Thanks for all your answer.
Get it.
Topic archived. No new replies allowed.