请帮忙看一段c线程的程序问题 谢谢
发表于 : 2009-09-07 21:31
#include <stdio.h>
#include <pthread.h>
void* t1_p(void* unused)
{
int i;
for (i = 0; i < 100; i++)
printf("a");
}
void* t2_p(void* unused)
{
int i;
for (i = 0; i < 100; i++)
printf("b");
}
int main()
{
pthread_t t1;
pthread_t t2;
pthread_create(&t1, NULL, t1_p, NULL);
pthread_create(&t2, NULL, t2_p, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
原意是使用该程序生成两个线程分别打印100个a和b。按照我原来的理解这两个线程是并行执行的。a和b的打印顺序应该是随机交错的。但是试了几次发现却是按照线程的创建顺序打印了100个a,然后是100个b。为什么是这样呢?是我的理解哪里有不对吗?谢谢!
#include <pthread.h>
void* t1_p(void* unused)
{
int i;
for (i = 0; i < 100; i++)
printf("a");
}
void* t2_p(void* unused)
{
int i;
for (i = 0; i < 100; i++)
printf("b");
}
int main()
{
pthread_t t1;
pthread_t t2;
pthread_create(&t1, NULL, t1_p, NULL);
pthread_create(&t2, NULL, t2_p, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
原意是使用该程序生成两个线程分别打印100个a和b。按照我原来的理解这两个线程是并行执行的。a和b的打印顺序应该是随机交错的。但是试了几次发现却是按照线程的创建顺序打印了100个a,然后是100个b。为什么是这样呢?是我的理解哪里有不对吗?谢谢!