LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 694|回复: 4

多线程问题

[复制链接]
发表于 2007-5-13 21:19:53 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void thread(void)
{
    int i;
        for(i=0;i<3;i++)
        printf("this is a pthread\n");
}
int main(void)
{
  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);
}

这个程序的结果为什么每次都是这样的?
this is the main process
this is the main process
this is the main process
this is a pthread
this is a pthread
this is a pthread
  看了很多资料都不是这样的,请问是什么问题?我的系统是FC6,gcc 版本 4.1.1 20061011 (Red Hat 4.1.1-30)
发表于 2007-5-14 08:37:09 | 显示全部楼层
你把循环的次数改大一点,改成10000, 结果就不一样了!
回复 支持 反对

使用道具 举报

发表于 2007-5-14 10:08:18 | 显示全部楼层
在每个printf("this is the main process\n");前加上一个循环for(int j=0;j<2000000;j++);
回复 支持 反对

使用道具 举报

发表于 2007-5-16 11:22:22 | 显示全部楼层
你希望怎么样?
回复 支持 反对

使用道具 举报

发表于 2007-5-21 07:55:49 | 显示全部楼层
pthread_join(id,NULL);

這句不需要處理error 問題嗎??
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表