小编The*_*ere的帖子

如何修复 pthread_create 调用中的分段错误

我当前的代码有问题。我正在开发一个项目,在该项目中,我使用线程从终端读取一组文件,并了解单个文件组和总文件组中有多少行。我的问题是,当我运行代码时,我得到一个核心转储,当我通过 gdb 运行代码时,我在 pthread_create 调用中得到分段错误。是因为我的实现还是因为我的代码中的其他内容?

#define NUM_THREADS 12
struct thread_data{
    char *thread_id;
    int count;
};

struct thread_data thread_data_array[NUM_THREADS];

void* filecount(void * thread_arg){
    char thread_id;
    int count;
    struct thread_data *thread;

    thread = (struct thread_data *) thread_arg;
    thread_id = *thread->thread_id;
    count = thread->count;

    FILE *fp = fopen(&thread_id, "r");
    if (fp == NULL) {
        fprintf(stderr, "Cannot open %s\n", thread_id);
        exit(-1);
    }

    for (char c = getc(fp); c != EOF; c = getc(fp))
        if (c == '\n')
            count++;

    fclose(fp);
    pthread_exit(NULL);
}

int main(int argc, …
Run Code Online (Sandbox Code Playgroud)

c pthreads

1
推荐指数
1
解决办法
63
查看次数

标签 统计

c ×1

pthreads ×1