免费注册 查看新帖 |

Chinaunix

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

请教多线程问题:为何打印参数与传递的不一致? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-01 16:07 |只看该作者 |倒序浏览
创建了四个线程,打印传递过去的参数。传递参数为 0,1,2,3,为何打印结果有时是1 ,2, 3,  2 呢?

——————————————————
#include <stdio.h>
#include <pthread.h>

void * prinfthread(int *arg)
{
        int *num;
        num = (int *)arg;
        printf("this is thread:%d\n",*num);
}

int main(int argc,char *argv[])
{
        pthread_t pth[4];
        int *attr ;
        int i,j;
        printf("this is main.\n");

        for(i = 0;i<4;i++)
        {
                attr = &i;
                pthread_create(&pth,NULL,(void *)prinfthread,(void *)attr);
        }
        for(j =0;j<4;j++)
        {
                pthread_join(pth[j],NULL);
        }



        return 0;
}
————————————————————
SuSE9Sp2:/myproject/lesson # ./thread
this is main.
this is thread:1
this is thread:2
this is thread:3
this is thread:2
SuSE9Sp2:/myproject/lesson # ./thread
this is main.
this is thread:1
this is thread:2
this is thread:3
this is thread:4

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
2 [报告]
发表于 2008-12-01 16:24 |只看该作者
原帖由 skyprince 于 2008-12-1 16:07 发表
创建了四个线程,打印传递过去的参数。传递参数为 0,1,2,3,为何打印结果有时是1 ,2, 3,  2 呢?

——————————————————
#include
#include

void * prinfthread(int *arg)
{
...

线程执行num = (int *)arg;的时候,arg (也就是(void *)attr)被修改了。attr = &i;

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
3 [报告]
发表于 2008-12-01 16:33 |只看该作者
线程中的num实际上取到的是主线程中i的值。i在主线程的for循环中被修改。

论坛徽章:
0
4 [报告]
发表于 2008-12-01 17:11 |只看该作者

回复 #3 ynchnluiti 的帖子

i 在循环中分别赋值为 0、1、2、3 ,怎么打印出来是 1、2、3、2,还有1、2、3、4?
能否控制只打印出 0,1,2,3呢。

论坛徽章:
0
5 [报告]
发表于 2008-12-01 17:20 |只看该作者
原帖由 skyprince 于 2008-12-1 17:11 发表
i 在循环中分别赋值为 0、1、2、3 ,怎么打印出来是 1、2、3、2,还有1、2、3、4?
能否控制只打印出 0,1,2,3呢。

编程中我最反感这种线程之间无关而却要使用共工变量的写法。到时候问题都不知道怎么找。
如果要使用全局变量通讯的话,最好加锁。
如果每个线程独立的话,在创建线程时,把参数赋值一份,传递给每个线程,例如

  1. struct thread_param_t
  2. {
  3.     int number;
  4. }
复制代码

然后在你创建线程之前给number赋值。
而且线程体参数的个数要和线程个数一样多。
这样写看得好累啊

论坛徽章:
0
6 [报告]
发表于 2008-12-01 17:27 |只看该作者
或者这么写,不要传地址了。
void * prinfthread(int *arg)
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int num;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = (int )arg;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("this is thread:%d\n",num);
&nbsp;&nbsp;&nbsp;}
............................
pthread_create(&pth,NULL,(void *)prinfthread,(void *)(i));

论坛徽章:
0
7 [报告]
发表于 2008-12-02 19:07 |只看该作者
就算这么写了,也未必正确。哪个线程先执行是不一定的。

论坛徽章:
0
8 [报告]
发表于 2008-12-03 10:37 |只看该作者
原帖由 Sorehead 于 2008-12-2 19:07 发表
就算这么写了,也未必正确。哪个线程先执行是不一定的。

是的,但是各个线程打印的结果并集起来应该是1,2,3,4,交集起来应该是空
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP