linux pthread问题
程序代码:
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> void pthread_run() { int i; for( i = 0; i < 3; i++ ) { printf("this is a pthread\n"); } } int main() { pthread_t id; int i; int ret; ret = pthread_create(&id, NULL, (void *)pthread_run, NULL ); if( ret ) { printf("pthread create error!!"); exit(1); } for( i = 0; i < 3; i++ ) printf("this is main thread\n"); pthread_join( id, NULL ); return (0); } 这是一个简单的pthread程序,编译通过,运行的时候按道理说应该是“this is main thread”和“this is a pthread”乱序输出的,结果多次试验都是先输出三遍this is main thread,然后再输出this is a pthread,这是为什么?