LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2008, 04:59 PM   #1
carlcs
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Rep: Reputation: 0
Thumbs up pthread_detach problem


Please check the code

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

Last edited by carlcs; 05-02-2008 at 05:00 PM.
 
Old 05-02-2008, 09:26 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
That's purely random chance. It's not "wrong."
 
Old 05-03-2008, 05:27 PM   #3
carlcs
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks for answer. I got it.
 
Old 07-23-2008, 04:01 AM   #4
lekhakr
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Rep: Reputation: 0
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.
 
Old 07-23-2008, 07:49 PM   #5
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
Quote:
Originally Posted by lekhakr View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration