tags:

views:

33

answers:

3

Hi, I am new to multi-threaded programming and have a question about pthreads.

This is the test code that I run and I don't understand its behaviour. Can someone throw some light on it please.

void *t1(void *args){  
    printf("returning from t1\n");  
    return;  
}  

void *t2(void *args){  
    printf("returning from t2\n");  
    return;  
}  

int main(){  
    pthread_t thread1,thread2;      
    int r1,r2;  
    r1=pthread_create(&thread1,NULL,t1,NULL);  
    r2=pthread_create(&thread2,NULL,t2,NULL);  

    pthread_join(thread1,NULL);    
 // pthread_join(thread2,NULL);   

    return 0;  
}  

The behaviour of this program is either of the 5 shown below

murtuza@murtuza:FFTW$ ./ptest  
returning from t2  
returning from t1  
murtuza@murtuza:FFTW$ ./ptest  
returning from t1  
returning from t2  
murtuza@murtuza:FFTW$ ./ptest  
returning from t1  
murtuza@murtuza:FFTW$ ./ptest  
returning from t2  
returning from t2  
murtuza@murtuza:FFTW$ ./ptest  
returning from t1  
returning from t2  
returning from t2  

I don't understand the 4th and 5th output. Why is thread t2 executing twice? Of course, if I uncomment pthread_join(&thread2,NULL,t2,NULL) the program will behave correctly but I am specifically interested in the case where only one thread joins the main() thread.

thanks, Mir

A: 

I'm afraid I wasn't able to replicate your problem.

I ran:

#include <pthread.h>
#include <stdio.h>

void *t1(void *args){  
    printf("returning from t1\n");  
    return NULL;  
}  

void *t2(void *args){  
    printf("returning from t2\n");  
    return NULL;  
}  

int main(){  
    pthread_t thread1,thread2;      
    int r1,r2;  
    r1=pthread_create(&thread1,NULL,t1,NULL);  
    r2=pthread_create(&thread2,NULL,t2,NULL);  

    pthread_join(thread1,NULL);    
 // pthread_join(thread2,NULL);   

    return 0;  
}  

As:

while (true) ; do ./ptest ; date ; done

And spotted: t1,t2 ; t2,t1 and t1.

But never repeated entries, or missing t1.

Sorry.

Maybe there's something broken in your threading library, or in printing out from a threaded process?

Douglas Leeder
+1  A: 

It may be that thread t2 is NOT executing twice, but the stdio library is printing the output twice because there is a race condition when two threads call printf() without any locking. You might try putting the calls to printf() inside a pthread_mutex_lock()/pthread_mutex_unlock() pair (both locking the same mutex, of course) and see if that causes the symptom to go away.

Jeremy Friesner
I did that but it still happens so I guess the reason is something else.
Meeir Aalie
A: 

I think you want us to explain undefined behaviour here. You should never use any C library function after you leave main(). I think that what you are seeing is the main() thread flushing buffers while it is shutting down the C library. I think it might be ignoring any streams locks at the time it is shutting down.

wilx
I think your explanation might be right as a pthread_join() for thread2 or even a sleep() in main() gets rid of the problem.
Meeir Aalie