ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define N 16
void *thread(void *vargp);
int main()
{
pthread_t tid;
int i,*ptr;
for (i = 0; i < N; i++)
{
ptr=malloc(sizeof(int));
*ptr=i;
pthread_create(&tid, NULL, thread, ptr);
}
sleep(3); /*wait all peer threads to exit*/
return 0;
}
/* thread routine */
void *thread(void *vargp)
{
int myid = *((int *)vargp);
pthread_detach(pthread_self());
free(vargp);
printf("Hello from thread %d, thread id is %ld\n", myid,pthread_self());
return NULL;
}
when you execute it .
it print the thread id number like this:
Hello from thread 0, thread id is -1208124528
Hello from thread 1, thread id is -1218614384
Hello from thread 2, thread id is -1229104240
Hello from thread 3, thread id is -1239594096
Hello from thread 4, thread id is -1250083952
Hello from thread 5, thread id is -1260573808
Hello from thread 6, thread id is -1271063664
Hello from thread 7, thread id is -1218614384
Hello from thread 8, thread id is -1208124528
Hello from thread 9, thread id is -1281553520
Hello from thread 10, thread id is -1292043376
Hello from thread 11, thread id is -1302533232
Hello from thread 12, thread id is -1302533232
Hello from thread 13, thread id is -1292043376
Hello from thread 14, thread id is -1281553520
Hello from thread 15, thread id is -1208124528
you can see that, lots of thread id are the same.
if i use the joinable function, it won't have this problem. What's the problem is the pthread_detach func? Am I write this program wrong.
I think as this is detached thread once the thread exits its id can be reused for some other thread. Hence u see the thread id being same.
That is exactly what's happening; i.e. reuse because the thread/process no longer exists. Now, if the thread type was 'joinable' it would persist in zombie mode until joined, and the id's could/would not be reused! Although pthread_create(pthread_detach()) and pthread_join() may not be compatible.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.