Linux pthread(分段错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8018272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:03:09  来源:igfitidea点击:

pthread (segmentation fault)

clinuxunixpthreads

提问by IKS

I'm Korean and I'm not good at English but if you give me a comment down there
I will be very pleased and will try to understand it.

我是韩国人,英语不好,但如果你在下面给我评论,
我会很高兴,并会尝试理解它。

I created, for example, 10 threads and tried to join them after creation and return the value.
But when I join the last thread, I get a segmentation fault.

例如,我创建了 10 个线程并尝试在创建后加入它们并返回值。
但是当我加入最后一个线程时,出现分段错误。

The result comes out like this..

结果出来了。。

Before Thread 1 create
After Thread 1 create
Before Thread 0 create
After Thread 0 create
Before Thread 1 join
After Thread 1 join
Before Thread 0 join
Segmentation Fault(core dumped)

when I create 4 threads it's like

当我创建 4 个线程时,就像

Before Thread 3 create
After Thread 3 create
Before Thread 2 create
After Thread 2 create
Before Thread 1 create
After Thread 1 create
Before Thread 0 create
After Thread 0 create
Before Thread 3 join
After Thread 3 join
Before Thread 2 join
After Thread 2 join
Before Thread 1 join
After Thread 1 join
Before Thread 0 join
Segmentation Fault(core dumped)

I can't seem to find why.

我似乎无法找到原因。

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

pthread_mutex_t mutex_lock;

struct arg_struct {
        int a;
        int b;
};

void *t_function(void *data) {
        pthread_mutex_lock(&mutex_lock);

        struct arg_struct *arg = (struct arg_struct *)data;
        long int s;

        s = arg->a;

        pthread_mutex_unlock(&mutex_lock);

        return (void **)s;
}

int main()
{
        int i;

        pthread_t p_thread[2];
        int thr_id;
        int status;

        struct arg_struct arg[2];

        for(i = 1; i >= 0; i--) {
                arg[i].a = i;
                arg[i].b = i;
        }

        pthread_mutex_init(&mutex_lock, NULL);

        for(i = 1; i >= 0; i--) {
                printf("Before Thread %d create\n", i);
                thr_id = pthread_create(&p_thread[i],NULL, t_function, (void *)&arg[i]);
                printf("After Thread %d create\n", i);
                usleep(1000);
        }

        int temp[2];

        for(i = 1; i >= 0; i--) {
                printf("Before Thread %d join\n", i);
                pthread_join(p_thread[i], (void**)&status);
                printf("After Thread %d join\n", i);
                temp[i] = status;
        }i

        printf("%d%d", temp[1], temp[0]);

        pthread_mutex_destroy(&mutex_lock);

        return 0;
}

采纳答案by bdonlan

    pthread_t p_thread[2];
    struct arg_struct arg[2];
    int temp[2];

You only allocated space for two elements here, so if you launch more than 2 threads you'll run off the end of the array and potentially crash or corrupt the stack.

您在这里只为两个元素分配了空间,因此如果您启动 2 个以上的线程,您将运行到数组的末尾并可能导致堆栈崩溃或损坏。

Additionally:

此外:

            pthread_join(p_thread[i], (void**)&status);

statusis an int, not a void *; attempting this will try to store a void *in an int. On many 64-bit platforms, this will also overflow (since void *will be 8 bytes while intis 4). Make statusa void *, and stop trying to cast away compiler errors like this. They're errors for a reason.

status是一个int,不是一个void *;尝试这样做将尝试将 a 存储void *int. 在许多 64 位平台上,这也会溢出(因为void *将是 8 个字节,而int为 4个字节)。制作status一个void *,并停止尝试抛弃这样的编译器错误。他们是有原因的错误。