标题:linux pthread问题
取消只看楼主
snowy_way
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-12-5
结帖率:100%
已结贴  问题点数:20 回复次数:2 
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,这是为什么?
搜索更多相关主题的帖子: linux 
2012-12-05 16:16
snowy_way
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-12-5
得分:0 
回复 2楼 yuccn
谢谢你的回答,不过经测试该为300,依然是那样,sleep方案可以,我是在一本资料上看到的这段程序,然后书上给的结果就是两个打印语句是乱序的,而自己测试却并不是这样的,不知道为什么?难道是书上的linux内核较老?我用的内核较新,线程切换算法一不一样了?
2012-12-05 16:44
snowy_way
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-12-5
得分:0 
回复 4楼 yuccn
嗯,谢谢了,不在这上面纠结了
2012-12-05 16:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-389731-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.918563 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved