Linux线程创建顺序与执行顺序
#include<stdio.h>#include<pthread.h>void*myThread1(void){printf("Thisisthe1stpthread,...
#include <stdio.h>
#include <pthread.h>
void *myThread1(void)
{
printf("This is the 1st pthread,created by zieckey.\n");
}
void *myThread2(void)
{
printf("This is the 2st pthread,created by zieckey.\n");
}
int main()
{
int i=0, ret=0;
pthread_t id1,id2;
/*创建线程1*/
ret = pthread_create(&id1, NULL, (void*)myThread1, NULL);
if (ret)
{
printf("Create pthread error!\n");
return 1;
}
/*创建线程2*/
ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);
if (ret)
{
printf("Create pthread error!\n");
return 1;
}
pthread_join(id1, NULL);
pthread_join(id2, NULL);
return 0;
}
运行结果是
This is the 2st pthread,created by zieckey.
This is the 1st pthread,created by zieckey.
为什么是2先再1,还有就是创建多个线程,还是1最后显示,其他都是按照顺序显示的,例如
This is the 2st pthread,created by zieckey.
This is the 3st pthread,created by zieckey.
This is the 4st pthread,created by zieckey.
This is the 1st pthread,created by zieckey.
为什么??线程创建与执行关系是如何的?大神们快过来啊(T T)、!! 展开
#include <pthread.h>
void *myThread1(void)
{
printf("This is the 1st pthread,created by zieckey.\n");
}
void *myThread2(void)
{
printf("This is the 2st pthread,created by zieckey.\n");
}
int main()
{
int i=0, ret=0;
pthread_t id1,id2;
/*创建线程1*/
ret = pthread_create(&id1, NULL, (void*)myThread1, NULL);
if (ret)
{
printf("Create pthread error!\n");
return 1;
}
/*创建线程2*/
ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);
if (ret)
{
printf("Create pthread error!\n");
return 1;
}
pthread_join(id1, NULL);
pthread_join(id2, NULL);
return 0;
}
运行结果是
This is the 2st pthread,created by zieckey.
This is the 1st pthread,created by zieckey.
为什么是2先再1,还有就是创建多个线程,还是1最后显示,其他都是按照顺序显示的,例如
This is the 2st pthread,created by zieckey.
This is the 3st pthread,created by zieckey.
This is the 4st pthread,created by zieckey.
This is the 1st pthread,created by zieckey.
为什么??线程创建与执行关系是如何的?大神们快过来啊(T T)、!! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询