免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2252 | 回复: 13
打印 上一主题 下一主题

请问:多线程运行结果和理论为什么不同 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-06 11:11 |只看该作者 |倒序浏览
#include <stdio.h>
#include <pthread.h>
void thread(void)
{
        int i;
        for(i=0;i<10;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 a pthread.
This is a pthread.
This is a pthread.
This is a pthread.
This is a pthread.
This is a pthread.
This is a pthread.
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.
但是主/子线程应该是并发执行的,是随机交叉打印输出.为什么运行的结果和理论不同.

论坛徽章:
0
2 [报告]
发表于 2005-12-06 12:10 |只看该作者
这也是一种随机情况

论坛徽章:
0
3 [报告]
发表于 2005-12-06 12:17 |只看该作者
但是每次都是这样的结果,就不是随机的了.我运行的结果每次都一样.Why?

论坛徽章:
0
4 [报告]
发表于 2005-12-06 12:51 |只看该作者
你在输出之间加sleep看看就知道了

论坛徽章:
0
5 [报告]
发表于 2005-12-06 12:58 |只看该作者

  1. for(i=0;i<10;i++)
  2.                 printf("This is a pthread.\n");
复制代码


因为你执行的任务根本就不花什么时间,在为一个线程分配的时间片内就立即执行完了.你把i<10改到i < 10000000试一下。

加sleep的功能是强制引起线程的切换。

论坛徽章:
0
6 [报告]
发表于 2005-12-06 13:05 |只看该作者
原帖由 renstone921 于 2005-12-6 12:58 发表

  1. for(i=0;i<10;i++)
  2.                 printf("This is a pthread.\n");
复制代码


因为你执行的任务根本就不花什么时间,在为一个线程分配的时间片内就立即执行完了.你把i<10改到i < ...

同意。

论坛徽章:
0
7 [报告]
发表于 2005-12-06 13:10 |只看该作者
在这个文章中有详细的论述:
http://www-128.ibm.com/developer ... ix_threadapi/part1/

论坛徽章:
0
8 [报告]
发表于 2005-12-06 13:10 |只看该作者
这么短的代码,计算机的速度是多少?一定是随机数,除非加入同步和互斥,就不是随机了!

论坛徽章:
0
9 [报告]
发表于 2005-12-06 17:22 |只看该作者
在我的机器上运行是主程序始终运行在子线程的前面

> ./t2
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.
This is a pthread.
This is a pthread.

论坛徽章:
0
10 [报告]
发表于 2005-12-06 19:08 |只看该作者
明白了,谢谢大家的关注.依赛特小子所言甚是,我重写了代码,与期望的结果相同.
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

pthread_mutex_t mutex;
pthread_cond_t cond;
unsigned int count=0;

void *thread(void * arg)
{
        int i;
        for(i=0;i<50;i++)
        {
                printf("This is a pthread.\n");
                sleep(1);
                pthread_mutex_lock(&mutex);
                while(count == 0)
                        pthread_cond_wait(&cond, &mutex);
                count++;
                printf("This is a pthread and the count = %d.\n",count);
                pthread_mutex_unlock(&mutex);
        }
        return NULL;
}

int main(void)
{
        pthread_t id;
        int i,ret;
        pthread_mutex_init(&mutex, NULL);
        pthread_cond_init(&cond, NULL);
        ret=pthread_create(&id,NULL,thread,NULL);
        if(ret!=0){
                printf ("Create pthread error!\n");
                exit (1);
        }
        for(i=0;i<50;i++)
        {
                sleep(1);
                pthread_mutex_lock(&mutex);
                if(!(count == 0))
                        pthread_cond_signal(&cond);
                count--;
                printf("This is the main process and the count = %d.\n",count);
                pthread_mutex_unlock(&mutex);
        }
        pthread_join(id,NULL);
        pthread_cond_destroy(&cond);
        return (0);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP