- 论坛徽章:
- 1
|
有如下一段线程的代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void * thfn(void *argv)
{
int i;
for(i=0;i<3;i++)
printf("this is new thread process.\n");
}
int main(void)
{
pthread_t id;
int i , ret;
ret=pthread_create(&id,NULL,thfn,NULL);
if(ret !=0)
{
printf("create thread error\n");
exit(1);
}
for(i=0;i<3;i++)
{
printf("this is main thread process\n");
}
pthread_join(id,NULL);
return 0;
}
编译运行后结果为:
this is main thread process
this is main thread process
this is main thread process
this is new thread process.
this is new thread process.
this is new thread process.
网上说每次运行的结果都不一样,但我所不解的是在我的系统里为什么每次运行的结果都是一样的,每次都是主线程先运行,难道没有发生cpu抢占吗?
 |
|