免费注册 查看新帖 |

Chinaunix

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

[Linux资讯] 在Ubuntu14.04运行,结果不是期望值,不知哪里错,望各位大神指教 [复制链接]

论坛徽章:
2
操作系统版块每日发帖之星
日期:2016-04-14 06:20:0015-16赛季CBA联赛之北京
日期:2016-04-21 09:17:18
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-04-11 19:47 |只看该作者 |倒序浏览
#include"all.h"
typedef struct link
        {
                int data;
                struct link *next;
        }Node;

Node *head=NULL;

pthread_mutex_t  mut=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t   cnd=PTHREAD_COND_INITIALIZER;

void writequeue(void *arg)
        {
                        
                Node *p=NULL;
               
                p=(Node *)malloc(sizeof(struct link));
                if(p==NULL)
                        {
                                printf("Allocate space failed!\n");
                                exit(-1);
                        }
               
                p->data=(int)arg;
                p->next=NULL;
                pthread_mutex_lock(&mut);
                if(head==NULL)
                        head=p;
                else
                        {
                                p->next=head;
                                head=p;
                        }
                //pthread_cond_signal(&cnd);
                pthread_mutex_unlock(&mut);
                pthread_cond_signal(&cnd);
               
                pthread_exit(NULL);
        }

void readqueue(void *arg)
        {
                Node *p=NULL;
                pthread_mutex_lock(&mut);
                while(head==NULL)
                        pthread_cond_wait(&cnd,&mut);
               
                printf("Read the headueue from head: \n");
                for(p=head;p!=NULL;p=p->next)
                        {
                                printf("%d    \n",p->data);
                                
                        }
                printf("\n");
                pthread_mutex_unlock(&mut);
                pthread_exit(NULL);
        }

int main(void)
        {
                pthread_t tid[5];
                pthread_t tidrd;
                int i=5;
                int err;
               Node *p;

               

                for(i=0;i<5;i++)
                        {
                                err=pthread_create(&tid,NULL,(void *)writequeue,(void *)(i+1));
                                if(err!=0)
                                        {
                                                printf("Create pthread failed!\n");
                                                exit(-1);
                                        }
                        }
               
             sleep(1);
             for(p=head;p;p=p->next)
                        printf("%d  \n",p->data);
            
               
                        err=pthread_create(&tidrd,NULL,(void *)readqueue,NULL);
                        if(err!=0)
                                {
                                        printf("Create pthread failed!\n");
                                        exit(-1);
                                }
               
               
                for(i=0;i<5;i++)
                        pthread_join(tid,NULL);
                pthread_join(tidrd,NULL);
                pthread_mutex_destroy(&mut);
                pthread_cond_destroy(&cnd);
                exit(0);
        }

期望结果是5 4 3 2 1,但是结果是相反的,若去掉sleep(1),结果却是5,在gdb下结果是正确的。不知错哪里了,望各位大神不吝赐教!

论坛徽章:
2
操作系统版块每日发帖之星
日期:2016-04-14 06:20:0015-16赛季CBA联赛之北京
日期:2016-04-21 09:17:18
2 [报告]
发表于 2016-04-11 20:59 |只看该作者
该问题已被解决,只是解决方案不是很理解,主要原因是不知道内核的线程调度!

论坛徽章:
224
2022北京冬奥会纪念版徽章
日期:2015-08-10 16:30:32操作系统版块每日发帖之星
日期:2016-02-18 06:20:00操作系统版块每日发帖之星
日期:2016-03-01 06:20:00操作系统版块每日发帖之星
日期:2016-03-02 06:20:0015-16赛季CBA联赛之上海
日期:2019-09-20 12:29:3219周年集字徽章-周
日期:2019-10-01 20:47:4815-16赛季CBA联赛之八一
日期:2020-10-23 18:30:5320周年集字徽章-20	
日期:2020-10-28 14:14:2615-16赛季CBA联赛之广夏
日期:2023-02-25 16:26:26CU十四周年纪念徽章
日期:2023-04-13 12:23:1015-16赛季CBA联赛之四川
日期:2023-07-25 16:53:45操作系统版块每日发帖之星
日期:2016-05-10 19:22:58
3 [报告]
发表于 2016-04-12 07:07 |只看该作者
为何不用 cpp class

论坛徽章:
2
操作系统版块每日发帖之星
日期:2016-04-14 06:20:0015-16赛季CBA联赛之北京
日期:2016-04-21 09:17:18
4 [报告]
发表于 2016-04-12 08:52 |只看该作者
回复 3# action08


    我主攻的是C,cpp只是有简单的了解
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP