0

I understand pthread_detach(pid) that: "storage for the thread thread can be reclaimed when that thread terminate" (as per http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_detach.html) However, I understand that means that once the pid thread finishes executing, its memory will be freed, and we will not be able to run it again, or call join on it. However, I tried the following code:

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

void* myFunction (void* arg)
{
  printf("Hello World from thread!\n");

}

int main()
{
pthread_t tid;
pthread_create(&tid, NULL, myFunction, NULL);
//pthread_join(tid, NULL);

int isDetached = -10;
isDetached = pthread_detach(tid);
printf("Is my thread detached: %d\n", isDetached);

int i;
for (i = 0; i<15; i++)
    printf("%d\n", i);

pthread_create(&tid, NULL, myFunction, NULL);
pthread_join(tid, NULL);

for (i = 0; i<15; i++)
    printf("%d\n", i);

return 0;

}

And I get the following: result

If I understand correctly, since I did pthread_detach(tid), I should not be able to be able to do

pthread_create(&tid, NULL, myFunction, NULL);
pthread_join(tid, NULL);

after it, yet I did and it works perfectly. So, what is really the purpose of doing othread_detach(pid) if we can still run the thread and join it?

Thanks a lot!

Community
  • 1
  • 1
Ziad
  • 63
  • 1
  • 9
  • detached threads could still run when the main process gets terminated. – Serge Mar 12 '18 at 19:00
  • Note that `pthread_create` creates a new thread, it does not "run the thread again". You have not detached this new thread. (And you need to check the return value of` pthread_join()` if you want to see if it fails or succeeds.) – nos Mar 12 '18 at 19:10

3 Answers3

0

pthread_detach simply tells your program that the current instance of tid will not join, (pthread_join) again, and frees up any pthread handles & objects on the tid instance.

Since you called pthread_detach if you try to pthread_join that thread it will not since it has been released and disposed of.

I've added the pthread_join to your code right after the detach call and you can see nothing happens as expected.

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

void * myFunction (void *arg)
{
  printf ("Hello World from thread!\n");

}

int main ()
{
  pthread_t tid;
  pthread_create (&tid, NULL, myFunction, NULL);
  //pthread_join(tid, NULL);

  int isDetached = -10;
  isDetached = pthread_detach (tid);
  printf ("Is my thread detached: %d\n", isDetached);

  /* ADDED FOR STACK OVERFLOW */
  pthread_join (tid, NULL);

  int i;
  for (i = 0; i < 15; i++)
    printf ("%d\n", i);

  pthread_create (&tid, NULL, myFunction, NULL);
  pthread_join (tid, NULL);

  for (i = 0; i < 15; i++)
    printf ("%d\n", i);

  return 0;
}

I'm not sure what the confusion is but if you were expecting a different behavior when you called pthread_create a second time; note, this is really creating a fresh instance of tid for you. Therefore your second join call runs the thread.

Eat at Joes
  • 4,937
  • 1
  • 40
  • 40
  • That makes sense. Your last paragraph as well as the code just cleared everything up! – Ziad Mar 12 '18 at 20:53
0
pthread_t tid;
pthread_create(&tid, NULL, myFunction, NULL);

This creates a new thread and subsequent call topthread_detach(pid) detaches the thread created. Now

pthread_create(&tid, NULL, myFunction, NULL);

This creates a new thread and subsequently you have called a join which basically waits for the completion of the newly created thread, not for the one which has been detached.

You can check the code attached below. When you run this code the thread which has been detached is printing even after join is called i.e myfunc1 is still printing after the pthread_join.

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

void *myFunction2 (void *arg)
{
    printf("Hello World from func2!\n");

}

void *myFunction1 (void *arg) 
{
    while(1) {
        printf("Hello World from func1\n");
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, myFunction1, NULL);
    //pthread_join(tid, NULL);

    int isDetached = -10;
    isDetached = pthread_detach(tid);
    printf("Is my thread detached: %d\n", isDetached);

    int i;

    pthread_create(&tid, NULL, myFunction2, NULL);
    pthread_join(tid, NULL);

    for (i = 0; i<15; i++)
        printf("%d\n", i);

    return 0;
}
0

Your variable, tid, is not a thread. Your program does not start one thread two times, it creates two completely different threads.

The actual threads are objects that exist in the operating system. Your tid variable is just a "handle" that you can use to interact with a thread. Your second call to pthread_create(&tid,...) is re-using the handle variable, pointing it to a new, different thread.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • Ohhhhh. Wow. Thanks a lot, that puts everything in place. I understand now. I thought tid is always responsible for the same threat. A thousand thanks again xD – Ziad Mar 12 '18 at 20:54