Doubt on pthread_exit and pthread_join


 
Thread Tools Search this Thread
Top Forums Programming Doubt on pthread_exit and pthread_join
# 1  
Old 05-25-2009
Doubt on pthread_exit and pthread_join

Main function creates Thread0 and Thread1 by using pthread_create systemcall.

In Thread0() {
we are calling pthread_exit(0) ;
}

and in Thread1() {
status= pthread_join(tid[0],NULL);
sprintf(ebuf,"timer6: can't join with thread0, status: %d",status);
Assert(status==0,ebuf);
}

My doubt is Will pthread_join throws errors(can't join with thread0) because Thread0 terminated and again trying to join with Thread1 right??
Please let me know..


Below problem happens sometimes,but not reproducible everytime.

Failure message:
==============
timer6: can't join with thread0, status: 3


I have a doubt,that through pthread_exit(0); will Thread0 terminates?? If Thread0 terminates then why to call pthread_join function again??
Please clarify..

Am not able to get the answer..Please help me in understanding it.


Thanks in Advance,
Mansa
# 2  
Old 05-25-2009
I think this error
Code:
Failure message:
==============
timer6: can't join with thread0, status: 3

occurs whenever thread0 dies first, or can say scheduled first.

Run the code again, and make the thread1 to wait. U will always get this error message.
# 3  
Old 05-25-2009
Thanks for your reply and clarification that problem is reproducible everytime..


I have a doubt,that if we call pthread_exit(0); then Thread0 terminates definately right?? If Thread0 terminates then in Thread1() why to call pthread_join function again??What's the use of calling pthread_join() in Thread1().Could you please clarify??


Pthread_join()-waits for the termination of target thread(here Thread0).
But in our case,thread0 terminated right.

Thanks,
Mansa
# 4  
Old 05-25-2009
the pthread_join call suspends the calling thread until a join is complete. When the join is done, the caller receives the joined thread’s termination status as the return from pthread_join. The pthread_join function (somewhat equivalent to the wait function for processes(fork)).
# 5  
Old 05-25-2009
Mansa, I suspect the "join" in pthread_join() is confusing you.

In POSIX threads terminology a join is actually a wait i.e. the calling thread (in your case thread 1) blocks until the specified thread has exited (in your case thread 0). It is equivalant to Win32's NTWaitForSingleObject()/WaitForSingleObject() or the old OS/2 DosWaitThread().

Not all threads can be joined. If you specified a detached thead at creation time, such a thread cannot be joined. Also any thread can call pthread_join() on any other thread but the exiting thread can only be joined once.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doubt..

Hi experts, In one of our code we have used some command like this. name=${name##*/} Can someone please let me know what exactly it does? Thanks & Regards, Sathya V. (1 Reply)
Discussion started by: Sathya83aa
1 Replies

2. Red Hat

Doubt

How to create a file with specific size in RHEL6 (1 Reply)
Discussion started by: Sashi Kanth A
1 Replies

3. Red Hat

doubt

I need help in opening the .exe files in linux. As i have downloaded ubuntu os from trail version. after executing the the file name in terminal it is stating that get archive files for opening the .exe files. But i am unable to get it please help me (2 Replies)
Discussion started by: yashwanthguru
2 Replies

4. Programming

pthread_exit query

Hi, I am new to multithreaded programming. When creating a thread to run a specific function, does the function need to have a pthread_exit ? Is pthread_exit analogous to a return in a function? (3 Replies)
Discussion started by: sanjayc
3 Replies

5. Shell Programming and Scripting

Doubt

Hi, I have a file with multiple entries and I have calculated the percentages. Now I want to know how many of my entries are there between 1-10% 11-20% and so on.. chr1_14401_14450 0.211954217888936 chr1_14451_14500 1.90758796100042 chr1_14501_14550 4.02713013988978... (1 Reply)
Discussion started by: Diya123
1 Replies

6. Programming

pthread_exit and pthread_join usage

Hi... this simple code has warning messages but it work.. void *th(void *g){ int y = 10; pthread_exit((void*)y); } int main(int argn, char ** argp){ void * ret; pthread_t thread; pthread_create(&thread, NULL, th, NULL); pthread_join(thread,... (10 Replies)
Discussion started by: prompt
10 Replies

7. UNIX for Advanced & Expert Users

pthread_join function

Hi, I would like to know if the call of pthread_join( thread,&status) for a thread already created in main function will free the memory allocated to thread after the pthread_join retruns or should I wait the termination of main function? Is there any need to cancel or exit the thread if I... (0 Replies)
Discussion started by: Behnaz
0 Replies

8. Linux

pthread_join hang on glibc2.5

Hello All, The problem i'm experiencing is with the following code: #include <pthread.h> #include <stdio.h> int main(void) { (void) pthread_join(155555, NULL); printf("done"); return 0; } I'm getting on terminal segmentation fault . System used:... (0 Replies)
Discussion started by: crocodil
0 Replies

9. UNIX for Advanced & Expert Users

FATAL:exception occured with pthread_exit()

We had written an application in which we create worker thread. So the main thread will create the worker thread. After some time the child thread(Worker thread) will call pthread_exit(). This function was written in try{} and there occured an Exception and is handled in catch(...)... (0 Replies)
Discussion started by: platso
0 Replies

10. UNIX for Dummies Questions & Answers

How to write my own version of pthread_join()

I would like to write my own version of pthread_join and some of the other pthread function. Does some know any pages that have som examples of doing this?? (1 Reply)
Discussion started by: bigblop
1 Replies
Login or Register to Ask a Question