简体   繁体   English

pthreads和pthread_join函数的行为

[英]behaviour of pthreads and pthread_join function

I am new to multi-threaded programming and have a question about pthreads. 我是多线程编程的新手,对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 该程序的行为是以下所示的5个行为之一

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? 为什么线程t2执行两次? 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. 当然,如果我取消注释pthread_join(&thread2,NULL,t2,NULL) ,程序将正确运行,但是我对仅一个线程加入main()线程的情况特别感兴趣。

thanks, Mir 谢谢,米尔

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 ; 并发现:t1,t2; t2,t1 and t1. t2,t1和t1。

But never repeated entries, or missing t1. 但不要重复输入,也不要丢失t1。

Sorry. 抱歉。

Maybe there's something broken in your threading library, or in printing out from a threaded process? 也许您的线程库有问题,或者从线程进程中打印出来了?

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. 可能是线程t2没有执行两次,但是stdio库将输出打印了两次,因为当两个线程在没有任何锁定的情况下调用printf()时,存在竞争条件。 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. 您可以尝试将对printf()的调用放在pthread_mutex_lock()/ pthread_mutex_unlock()对内(当然,两者都锁定同一个互斥锁),看看是否会导致症状消失。

I think you want us to explain undefined behaviour here. 我认为您希望我们在这里解释未定义的行为 You should never use any C library function after you leave main() . 离开main()之后,切勿使用任何C库函数。 I think that what you are seeing is the main() thread flushing buffers while it is shutting down the C library. 我认为您正在看到的是关闭C库时的main()线程刷新缓冲区。 I think it might be ignoring any streams locks at the time it is shutting down. 我认为它可能会在关闭时忽略任何流锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM