level 1
/* thread.c*/
#include <stdio.h>
#include <pthread.h>
void thread(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread.\n");
}
int main(int argc,char *argv[])
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++)
printf("This is the main process.\n");
pthread_join(id,NULL);
return (0);
}
2011年11月16日 16点11分
2
level 1
请问我编译之后:$gcc thread.c -lpthread -o thread
按理来说结果应该是:
得到如下结果:
This is the main process.
This is a pthread.
This is the main process.
This is the main process.
This is a pthread.
This is a pthread.
再次运行,可能得到如下结果:
This is a pthread.
This is the main process.
This is a pthread.
This is the main process.
This is a pthread.
This is the main process.
每次应该是不一样的,可是我每次都是一样的
This is a pthread.
This is a pthread.
This is a pthread.
This is the main process.
This is the main process.
This is the main process.
每次都是这一个结果,请问这是怎么回事?
2011年11月16日 16点11分
3
level 10
哥,我知道了,你不会是每次都运行的是第一次gcc编译出的thread文件吧?
2012年08月20日 12点08分
6
level 6
多线程吗?简单啊,分配好时间,位置,...
按次序循环来,和lun*jian没区别
2012年08月24日 14点08分
8