程序:
#include
#include
#include
#include
pthread_t pid;
pthread_t tpid;
int j = 0;
void tpidFunction()
{
printf(".......thread id is %d\n",(int)pthread_self());
}
void pidFunction()
{
printf("this is pthread..%d.\n",j);
j ++;
pthread_create(&tpid,NULL,(void *)tpidFunction,NULL);
}
int main()
{
int i;
for(i = 0;i < 10;i++)
{
pthread_create(&pid,NULL,(void *)pidFunction,NULL);
}
return 0;
}
执行结果:
this is pthread..0.
this is pthread..0.
this is pthread..0.
this is pthread..0.
this is pthread..0.
this is pthread..2.
.......thread id is 178221056
this is pthread..3.
.......thread id is 179294208
this is pthread..3.
this is pthread..3.
.......thread id is 180903936
this is pthread..3.
.......thread id is 181977088
.......thread id is 182513664
.......thread id is 183050240
.......thread id is 183586816
问题:
1. 为什么第一个进程中循环10次,打印了10次,同时在第一个进程中启动了另外一个子进程,为什么只打印了8次
2.其中有一个变量j,为什么不是打0.1.2.3......9?