亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1686 | 回復(fù): 6
打印 上一主題 下一主題

[C] 這樣傳遞指針為什么不行?謝謝先 [復(fù)制鏈接]

論壇徽章:
1
白羊座
日期:2014-01-03 23:26:39
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2009-08-06 11:51 |只看該作者 |倒序?yàn)g覽
運(yùn)行斷錯(cuò)誤

#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 [報(bào)告]
發(fā)表于 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 [報(bào)告]
發(fā)表于 2009-08-06 11:58 |只看該作者

回復(fù) #2 hellioncu 的帖子

我后頭改為你這樣的,但是我想知道為什么之前的不行,在這個(gè)問題上已經(jīng)碰到兩次了。

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2009-08-06 12:05 |只看該作者
這就是傳說中的野指針?

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2009-08-06 12:59 |只看該作者
原帖由 zphjita 于 2009-8-6 11:58 發(fā)表
我后頭改為你這樣的,但是我想知道為什么之前的不行,在這個(gè)問題上已經(jīng)碰到兩次了。


因?yàn)閍rg是指針,你沒有給它分配空間就給它的成員fp賦值,所以段錯(cuò)誤了?梢韵扔胢alloc之類的給arg分配空間。

論壇徽章:
1
白羊座
日期:2014-01-03 23:26:39
6 [報(bào)告]
發(fā)表于 2009-08-06 13:39 |只看該作者

回復(fù) #5 towardWang 的帖子

原來這樣 謝謝

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2009-08-06 13:43 |只看該作者

回復(fù) #5 towardWang 的帖子

正解。
我覺得小結(jié)構(gòu)這樣也不錯(cuò)啦
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。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP