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.
hello everyone, i am making a program using threads and i've got the following function and it prints for the same thread the same tid but in different iteration.. i can't understand how it works.. there shouldn't be different tids for the father for each iteration? Please someone tell me what those tids are for each iteration.. thank u!
void *function(void *num)
{
pthread_t tid;
int i = (int) num;
while(i>0) {
i--;
pthread_create(&tid, NULL, function, (void *) i);
printf("%d %d %d\n", i, pthread_self(),tid);
pthread_join(tid, NULL); }
pthread_exit(NULL); }
When a thread is invoked using pthread_create(), a thread ID, or TID, is returned. When calling pthread_self() from within the thread, the value returned should be the same as the TID obtained by the parent.
Running your code with a small value of 'i' (in my test, set to 2), I got the following results:
Code:
140164009686784 140164009678592 <--- From the parent thread
1 140164009678592 140164001285888 <--- For the first thread created (i = 2, then decremented)
0 140164009678592 140163992893184 <--- For the second thread created(i = 1, then decremented)
0 140164001285888 140163984500480 <--- For the third thread created(i = 1, then decremented)
The first column above is the value of 'i' (after it was decremented), the second column being the TID returned by pthread_self(), and lastly the third column is the TID returned by pthread_create(). You can see from the first thread above, that the TID is the same as the value returned by pthread_self() in the third thread.
P.S. Here's the code I test with, which btw, will generate warnings on an x86-64 system:
Code:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void* function(void* num)
{
pthread_t tid;
int i = (int) num;
while (i > 0)
{
--i;
pthread_create(&tid, NULL, function, (void*) i);
printf("%d %lu %lu\n", i, pthread_self(), tid);
}
pthread_exit(NULL);
}
int main()
{
pthread_t tid;
pthread_create(&tid, NULL, function, (void*) 2);
printf(" %lu %lu\n", pthread_self(), tid);
sleep(1);
return 0;
}
Last edited by dwhitney67; 05-07-2012 at 06:02 AM.
Thank u very much but in my case it prints the following:
2 1099352384 1109842240
1 1109842240 1120332096
0 1120332096 1130821952
0 1109842240 1120332096
1 1099352384 1109842240
0 1109842240 1120332096
0 1099352384 1109842240
and i cant understand why it prints 1109842240 1120332096 for all the iterations.. i mean the second number shouldn't be different each time? :/
If you are still using your original program, then perhaps you need to change the "%d" format in the printf() to be "%u". If you are on an x86-64 system, then change the format to be "%lu" as I did in the program I included earlier.
i think the reason that in your case it prints different number its because you remove pthread_join because i did the same thing and now i take different tid for each iteration. I still don't understand what is happening :/
i think the reason that in your case it prints different number its because you remove pthread_join because i did the same thing and now i take different tid for each iteration. I still don't understand what is happening :/
Yes, I took out the pthread_join()... and sorry, I forgot to replace it with a pthread_detach(). Here's the revised function:
Code:
void* function(void* num)
{
...
while (i > 0)
{
...
}
pthread_detach(pthread_self());
return NULL;
}
In case you are wondering what the pthread_detach() does, here's the man-page description:
Code:
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.
ok thank u so much! sorry for being tiring but please can u tell me what happens in case i have join? why tids remain the same? thank u again!
I am not fully versed on the inner details of the pthread behavior, but I do know that pthread_join() will block the calling thread until the child-thread has terminated. It's possible that the system may reuse a TID previously assigned to a terminated thread, but I do not have any evidence to back this theory. Suffice to say, each thread that is currently running will have a unique TID.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.