为什么运行结果为下面的情况呢?求解释

代码如下:
#include<pthread.h>
#include<stdio.h>

#define MAX_THREADS 5

void *myThread(void *arg)
{
printf("Thread %d started\n", (unsigned int)arg);

pthread_exit(arg);
}

int main()
{
int ret, i, status;
pthread_t threadids[MAX_THREADS];

for(i=0; i<MAX_THREADS; i++)
{
ret=pthread_create(&threadids[i], NULL, myThread, (void*)i);
if(ret!=0)
printf("error creating thread %d\n", i);
}
for(i=0; i<MAX_THREADS; i++)
{
ret=pthread_join(threadids[i], (void** )&status);
if(ret!=0)
printf("error joining thread %d\n", i);
else
printf("status=%d\n",status);
}

return 0;
}

为什么运行结果为:
Thread 3 started
Thread 4 started
Thread 2 started
Thread 1 started
Thread 0 started
status=0
status=1
status=2
status=3
status=4

谢谢!

忽然笑
浏览 182回答 2
2回答

皈依舞

Linux系统pthread_join用于挂起当前线程(调用pthread_join的线程),直到thread指定的线程终止运行为止,当前线程才继续执行。案例代码:/*********************************************&nbsp;&nbsp;&nbsp;&nbsp;Name:pthread_join.c**&nbsp;&nbsp;&nbsp;&nbsp;用于Linux下多线程学习**&nbsp;&nbsp;&nbsp;&nbsp;案例解释线程的暂停和结束**&nbsp;&nbsp;&nbsp;&nbsp;Author:admin**&nbsp;&nbsp;&nbsp;&nbsp;Date:2015/8/11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**&nbsp;&nbsp;&nbsp;&nbsp;Copyright&nbsp;(c)&nbsp;2015,All&nbsp;Rights&nbsp;Reserved!**********************************************#include&nbsp;<pthread.h>#include&nbsp;<unistd.h>#include&nbsp;<stdio.h>void&nbsp;*thread(void&nbsp;*str){&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;&nbsp;&nbsp;&nbsp;&nbsp;//不调用pthread_join线程函数&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;10;&nbsp;++i)&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;"This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;%d\n"&nbsp;,&nbsp;i&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NULL;}&nbsp;int&nbsp;main(){&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;pth;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;ret&nbsp;=&nbsp;pthread_create(&pth,&nbsp;NULL,&nbsp;thread,&nbsp;(void&nbsp;*)(i));&nbsp;&nbsp;&nbsp;&nbsp;//调用pthread_join线程函数&nbsp;&nbsp;&nbsp;&nbsp;pthread_join(pth,&nbsp;NULL);&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;10;&nbsp;++i)&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;"This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;%d\n"&nbsp;,&nbsp;i&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;}通过Linux下shell命令执行上面的案例代码:[root@localhost&nbsp;src]#&nbsp;gcc&nbsp;pthread_join.c&nbsp;-lpthread[root@localhost&nbsp;src]#&nbsp;./a.outThis&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;0This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;0This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;1This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;2This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;1This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;3This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;4This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;2This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;5This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;6This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;3This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;7This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;8This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;4This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;9子线程还没有执行完毕,main函数已经退出,那么子线程也就退出了,“pthread_join(pth,&nbsp;NULL);”函数起作用。[root@localhost&nbsp;src]#&nbsp;gcc&nbsp;pthread_join.c&nbsp;-lpthread[root@localhost&nbsp;src]#&nbsp;./a.outThis&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;0This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;1This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;2This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;3This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;4This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;5This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;6This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;7This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;8This&nbsp;in&nbsp;the&nbsp;thread&nbsp;:&nbsp;9This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;0This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;1This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;2This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;3This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;4This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;5This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;6This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;7This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;8This&nbsp;in&nbsp;the&nbsp;main&nbsp;:&nbsp;9这说明pthread_join函数的调用者在等待子线程退出后才继续执行。&nbsp;

呼啦一阵风

这是随机情况,由系统调度决定,不是唯一的结果,你可以尝试这样改:ret=pthread_create(&threadids[i], NULL, myThread, (void*)i);sleep(1);这样就是按顺序创建线程
打开App,查看更多内容
随时随地看视频慕课网APP