LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-07-2012, 05:14 AM   #1
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Rep: Reputation: Disabled
thrs


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); }
 
Old 05-07-2012, 05:52 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 05-07-2012, 06:22 AM   #3
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
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? :/
 
Old 05-07-2012, 06:39 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 05-07-2012, 06:47 AM   #5
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
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 :/
 
Old 05-07-2012, 06:55 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by mcy View Post
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.
 
Old 05-07-2012, 07:04 AM   #7
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
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!
 
Old 05-07-2012, 08:35 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by mcy View Post
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.
 
Old 05-07-2012, 08:37 AM   #9
mcy
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
ok thank u very much!!
 
  


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



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

All times are GMT -5. The time now is 04: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