|
|
#include <stdio.h>
#include <pthread.h>
void thread (void)
{
int i,a[100],pid;
a[0]=1;
a[1]=1;
for (i=2;i<100;i++)
{a=a[i-1]+a[i-2];
pid=getpid();
printf("This is a thread and the %dth result is %d\n",i-1,a);
printf("pid is %d\n",pid);
}
}
int main(void)
{
pthread_t thread1,thread2;
int i,j,k,pid1;
j=pthread_create(&thread1,NULL,(void *)thread,NULL);
k=pthread_create(&thread2,NULL,(void *)thread,NULL);
if(j!=0||k!=0 )
{
printf("Creat pthread error!\n");
//exit(1);
}
for (i=0;i<3;i++)
{
printf("This is the main process\n");
pid1=getpid();
printf("The main process id is %d\n",pid1);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
}
return(0);
}
执行的结果始终是先打印一个线程的结果,再打印另一个线程的结果,为什么不是交替打印?只有交替打印才能显示两个线程是并行的啊。 |
|