在评论在pthread_join该线程同一个线程子程序的多个执行 [英] Multiple execution of same thread subroutine on commenting pthread_join for that thread
问题描述
我是新来穿线。
在这里,如果我评论在pthread_join(线程1,NULL),那么在输出有时我得到
线程2
线程1
线程1
我不能够理解为什么线程1轨迹来两次,是什么在pthread_join的确切功能。
另外,请参考上线程的概念对于初学者一些教程。
无效* print_message_function(无效* PTR);
主要()
{
的pthread_t线程1,线程;
字符* MESSAGE1 =线程1;
字符* MESSAGE2 =线程2;
INT iret1,iret2;
iret1 =在pthread_create(安培;线程1,NULL,print_message_function,(无效*)MESSAGE1);
iret2 =在pthread_create(安培;线程2,NULL,print_message_function,(无效*)消息2);
在pthread_join(线程1,NULL); 在pthread_join(线程2,NULL); 的printf(线程1返回数:%d \\ n,iret1);
的printf(线程2返回数:%d \\ n,iret2);
出口(0);
} 无效* print_message_function(无效* PTR)
{
字符*消息;
消息=(字符*)PTR;
的printf(%S \\ n,邮件);
}
如果我得到这些结果,首先我要做到以下几点:
1)代替下面的线条,
iret1 =在pthread_create(安培;线程1,NULL,print_message_function,(无效*)MESSAGE1);iret2 =在pthread_create(安培;线程2,NULL,print_message_function,(无效*)消息2);
在pthread_join(线程1,NULL);在pthread_join(线程2,NULL);
其替换,
iret1 =在pthread_create(安培;线程1,NULL,print_message_function,(无效*)MESSAGE1);在pthread_join(线程1,NULL);
iret2 =在pthread_create(安培;线程2,NULL,print_message_function,(无效*)消息2);在pthread_join(线程2,NULL);
和看到的结果是什么。
2)内的线程函数,你需要调用了pthread_exit(退出);这是从线程功能来退出有道。在函数结尾做到这一点。
无效* print_message_function(无效* PTR)
{
字符*消息;
消息=(字符*)PTR;
的printf(%S \\ n,邮件);
了pthread_exit(退出);
}
如果你用这种方法做它,最好你应该不会遇到任何问题。在任何情况下,我假设你正在使用 -D_REENTRANT的gcc -o threadex threadex.c -lpthread
这是不是最终的解决方案。如果进展顺利的话,我们可以继续在同一时间同时启动线程的下一个步骤。
请将这些更改后分享反馈。
I am new to threading. Here if I comment pthread_join( thread1, NULL) then in the output sometimes I get
Thread2
Thread1
Thread1
I am not able to understand why Thread1 trace is coming twice and what is the exact functionality of pthread_join.
Also, please refer some tutorial on threading concepts for beginners.
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
If I am getting these results, first of all I would do the following:
1) Instead of below lines,
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
replace it with,
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
pthread_join( thread1, NULL);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread2, NULL);
and see what is the result.
2) Inside your thread function, you need to call pthread_exit("Exit"); This is a proper way to exit from the thread function. Do it at the end of function.
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
pthread_exit("Exit");
}
If you are doing it this way, ideally you should not face any problem. In every case, i am assuming you are compiling your program using gcc -D_REENTRANT -o threadex threadex.c -lpthread
This is not the final solution. If it is going well, then we can proceed to next step of starting both the threads at a time.
Please share the feedback after incorporating these changes.
这篇关于在评论在pthread_join该线程同一个线程子程序的多个执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!