免费注册 查看新帖 |

Chinaunix

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

求助~~!关于linux的线程同步问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-21 21:05 |只看该作者 |倒序浏览
一个简单的生产者,消费者问题,请问为何在注释掉消费者和生产者线程中sleep()函数,就会出现段错误啊~我是CU新手,大家帮忙看一下,非常感谢~~!代码如下:
#include <stdio.h>
#include <pthread.h>
#include <malloc.h>
#include <time.h>
#include <error.h>
#define MAX_SIZE 40
typedef struct list{
        int i;
        struct list *next;
}list;
pthread_t tid;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t no_empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t no_full = PTHREAD_COND_INITIALIZER;
static list list_head = {
        .i = 0,
        .next = NULL,
};
static void *produce(){
        time_t t;
        srand((unsigned)time(&t));
        for(;{
                list *node;
                node = (list *)malloc(sizeof(list));
                node->i = (int)rand();
                node->next = NULL;
//                sleep(1);
                pthread_mutex_lock(&lock);
                if(list_head.i == MAX_SIZE)
                        pthread_cond_wait(&no_full, &lock);
                list_head.next = node;
                list_head.i += 1;
                printf("I Produce val=%d\n", node->i);
                pthread_mutex_unlock(&lock);
                pthread_cond_signal(&no_empty);               
        }       
}
static void *consume(){
        list *tmp;
        for(;{
//                sleep(1);
                pthread_mutex_lock(&lock);
                if(list_head.i == 0)
                        pthread_cond_wait(&no_empty, &lock);
                tmp = list_head.next;
                printf("I Consume val=%d\n",tmp->i);
                list_head.next = tmp->next;
                free(tmp);
                tmp = NULL;
                list_head.i -= 1;
                pthread_mutex_unlock(&lock);
                pthread_cond_signal(&no_full);
        }
}
int main(){
        pthread_t pro_tid;
        pthread_t con_tid;
        void *pret;
        void *cret;
        if(pthread_create(&pro_tid, NULL, produce, NULL) != 0){
                perror("pthread_create_pro_error:";
        }
        if(pthread_create(&con_tid, NULL, consume, NULL) != 0){
                perror("pthread_create_con_error:";
        }
        if(pthread_join(pro_tid, &pret) != 0){
                perror("join_pro_error:";
        }
        if(pthread_join(con_tid, &cret) != 0){
                perror("join_pro_error:";
        }
        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2011-11-21 21:58 |只看该作者
本帖最后由 digdeep126 于 2011-11-21 22:01 编辑

兄弟是你的链表操作有问题。在函数static void *produce()中的 list_head.next = node;前面加一句:node->next = list_head.next;
与线程代码没有关系。

  1. static void *produce()
  2. {
  3.         time_t t;
  4.         srand((unsigned)time(&t));
  5.         for(;;){
  6.                 list *node;
  7.                 node = (list *)malloc(sizeof(list));
  8.                 node->i = (int)rand();
  9.                 node->next = NULL;
  10. //                sleep(1);
  11.                 pthread_mutex_lock(&lock);
  12.                 printf("afte lock in produce\n");
  13.                 if(list_head.i == MAX_SIZE)
  14.                         pthread_cond_wait(&no_full, &lock);
  15.                 node->next = list_head.next;
  16.                 list_head.next = node;
  17.                 printf("afte list insert in produce\n");
  18.                 list_head.i += 1;
  19.                 printf("I Produce val=%d\n", node->i);
  20.                 pthread_cond_signal(&no_empty);
  21.                 pthread_mutex_unlock(&lock);
  22.         }
  23.         return NULL;
  24. }

复制代码
并且代码:
if(list_head.i == MAX_SIZE)
                        pthread_cond_wait(&no_full, &lock);
最好改成:
while(list_head.i == MAX_SIZE)
                        pthread_cond_wait(&no_full, &lock);

评分

参与人数 1可用积分 +2 收起 理由
lenky0401 + 2 感谢帮助

查看全部评分

论坛徽章:
0
3 [报告]
发表于 2011-11-21 22:02 |只看该作者
回复 2# digdeep126


    非常感谢~~!逻辑错误太可怕,脑袋都晕了,竟然犯了如此低级错误~~惭愧啊!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP