为什么即使我更改 pthread_join() 位置,我的输出顺序也不会改变
why the order of my output doesn't change even if i change my pthread_join() place
为什么 pthread_join( thread3, NULL);
在 pthread_join( thread1, NULL);
之前得到相同的输出顺序
什么时候:
pthread_join( thread1, NULL);
在 pthread_join( thread3, NULL);
之前
我知道 pthread_join()
等待线程终止并且它在线程 2 上工作但是为什么它在这里不起作用就像我等待线程 3 终止但它仍然在线程 1 终止后终止。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void * print_message_function( void *ptr );
void * do_something_else( );
void * a_new_function();
int main()
{
pthread_t thread1, thread2,thread3,thread4;
char *message1 = "Thread 1 is running ";
char *message2 = "Thread 2";
int iret1, iret2,iret3;
void * (*f_ptr)();
f_ptr = do_something_else;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, f_ptr/*do_something_else*/, NULL);
pthread_join( thread2, NULL);
iret3 = pthread_create( &thread3, NULL,a_new_function , NULL);
pthread_join( thread3, NULL); //--------------here-------------------------------
pthread_join( thread1, NULL); //---------------here-------------------------------
return 0;
}
void * print_message_function( void *ptr )
{
int i;
for(int i=0; i < 1; i++)
printf("%s \n", (char *)ptr);
}
void * a_new_function(){
int a;
int b;
int c;
a=5;b=7;
c=b+a;
printf ("the value of c =%d\n",c);
return 0;
}
void * do_something_else( )
{
int i;
for(int i=0; i < 1; i++)
printf("%s \n", "this is do_something_else function");
}
两种方式的输出为:
**这是do_something_else函数
线程 1 是 运行
c的值=12**
Pthread_join 不会影响您加入的线程的调度;实际上,它只是一种清理资源和检索退出状态的方法。
在您的示例中,唯一的依赖项是 thread 2
将在 thread 3
之前完成,因为您在加入 thread 2
.
之前不会创建 thread 3
这对thread 1
没有影响;并且最终连接的顺序无关紧要。
“等待 X”就是等待 X 发生。 “Waiting for Y”就是等到Y发生。在 X 或 Y 发生之前,等待 X 和等待 Y 之间没有区别。不清楚为什么您认为等待哪个线程有任何区别。等待对你正在等待的事情没有影响。它只是等待他们完成。
为什么 pthread_join( thread3, NULL);
在 pthread_join( thread1, NULL);
之前得到相同的输出顺序
什么时候:
pthread_join( thread1, NULL);
在 pthread_join( thread3, NULL);
我知道 pthread_join()
等待线程终止并且它在线程 2 上工作但是为什么它在这里不起作用就像我等待线程 3 终止但它仍然在线程 1 终止后终止。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void * print_message_function( void *ptr );
void * do_something_else( );
void * a_new_function();
int main()
{
pthread_t thread1, thread2,thread3,thread4;
char *message1 = "Thread 1 is running ";
char *message2 = "Thread 2";
int iret1, iret2,iret3;
void * (*f_ptr)();
f_ptr = do_something_else;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, f_ptr/*do_something_else*/, NULL);
pthread_join( thread2, NULL);
iret3 = pthread_create( &thread3, NULL,a_new_function , NULL);
pthread_join( thread3, NULL); //--------------here-------------------------------
pthread_join( thread1, NULL); //---------------here-------------------------------
return 0;
}
void * print_message_function( void *ptr )
{
int i;
for(int i=0; i < 1; i++)
printf("%s \n", (char *)ptr);
}
void * a_new_function(){
int a;
int b;
int c;
a=5;b=7;
c=b+a;
printf ("the value of c =%d\n",c);
return 0;
}
void * do_something_else( )
{
int i;
for(int i=0; i < 1; i++)
printf("%s \n", "this is do_something_else function");
}
两种方式的输出为:
**这是do_something_else函数
线程 1 是 运行
c的值=12**
Pthread_join 不会影响您加入的线程的调度;实际上,它只是一种清理资源和检索退出状态的方法。
在您的示例中,唯一的依赖项是 thread 2
将在 thread 3
之前完成,因为您在加入 thread 2
.
thread 3
这对thread 1
没有影响;并且最终连接的顺序无关紧要。
“等待 X”就是等待 X 发生。 “Waiting for Y”就是等到Y发生。在 X 或 Y 发生之前,等待 X 和等待 Y 之间没有区别。不清楚为什么您认为等待哪个线程有任何区别。等待对你正在等待的事情没有影响。它只是等待他们完成。