免费注册 查看新帖 |

Chinaunix

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

[C] 这样传递指针为什么不行?谢谢先 [复制链接]

论坛徽章:
1
白羊座
日期:2014-01-03 23:26:39
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-06 11:51 |只看该作者 |倒序浏览
运行断错误

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

struct arg_pthread
{
    FILE *fp;
    int num;
    pthread_mutex_t mutex;
};

void *thread(void *arg)
{
    struct arg_pthread *arg_t = (struct arg_pthread *)arg;
    int run;
    FILE *fp = arg_t->fp;

    if (pthread_mutex_lock(&(arg_t->mutex)) != 0)
    {
        perror("pthread_mutex_lock ");
        return NULL;
    }
   
    for (run=1; run<4; run++) {
        fprintf(fp, "In thread%d run %d\n", arg_t->num, run);
    }

unlock:
    if (pthread_mutex_unlock(&(arg_t->mutex)) != 0)
    {
        perror("pthread_mutex_unlock ");
        goto unlock;
    }
}

int main()
{
    FILE *readf = NULL;
    pthread_t myth1, myth2;
    struct arg_pthread *arg;

    readf = fopen("./testfile", "w+");
    if (readf == NULL)
    {
        perror("fopen ");
        exit(-1);
    }

    arg->fp = readf;
    arg->num = 1;

    if (pthread_mutex_init(&(arg->mutex), NULL) != 0)
    {
        perror("mutex init ");
        fclose(readf);
        exit(-1);
    }

    if (pthread_create(&myth1, NULL, thread, (void *)arg) != 0)
    {
        perror("pthread_create1 ");
        fclose(readf);
        exit(-1);
    }
   
    pthread_join(myth1, NULL);
    arg->num = 2;

    if (pthread_create(&myth2, NULL, thread, (void *)arg) != 0)
    {
        perror("pthread_create2 ");
        fclose(readf);
        pthread_cancel(myth1);
        exit(-1);
    }
   
   
    pthread_join(myth2, NULL);
    pthread_mutex_destroy(&(arg->mutex));

    fclose(readf);

    exit(0);
}

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2009-08-06 11:54 |只看该作者
struct arg_pthread arg;

arg.fp = readf;

if (pthread_create(&myth1, NULL, thread, (void *)&arg) != 0)

论坛徽章:
1
白羊座
日期:2014-01-03 23:26:39
3 [报告]
发表于 2009-08-06 11:58 |只看该作者

回复 #2 hellioncu 的帖子

我后头改为你这样的,但是我想知道为什么之前的不行,在这个问题上已经碰到两次了。

论坛徽章:
0
4 [报告]
发表于 2009-08-06 12:05 |只看该作者
这就是传说中的野指针?

论坛徽章:
0
5 [报告]
发表于 2009-08-06 12:59 |只看该作者
原帖由 zphjita 于 2009-8-6 11:58 发表
我后头改为你这样的,但是我想知道为什么之前的不行,在这个问题上已经碰到两次了。


因为arg是指针,你没有给它分配空间就给它的成员fp赋值,所以段错误了。可以先用malloc之类的给arg分配空间。

论坛徽章:
1
白羊座
日期:2014-01-03 23:26:39
6 [报告]
发表于 2009-08-06 13:39 |只看该作者

回复 #5 towardWang 的帖子

原来这样 谢谢

论坛徽章:
0
7 [报告]
发表于 2009-08-06 13:43 |只看该作者

回复 #5 towardWang 的帖子

正解。
我觉得小结构这样也不错啦
struct arg_pthread
{
    FILE *fp;
    int num;
    pthread_mutex_t mutex;
} arg_pthread;

struct arg_pthread *arg = &arg_pthread;


或者struct arg_pthread *arg = malloc(sizeof(struct arg_pthread));但要记得free。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP