【问题标题】:Only first value returned from thread is wrong (C)只有从线程返回的第一个值是错误的 (C)
【发布时间】:2021-06-13 14:31:33
【问题描述】:

所以我在做 C 编程作业时遇到了一个奇怪的线程行为: 我给一个线程一个值,然后它把它返回给pthread_exit() 函数,然后我在主函数中添加所有返回的值。问题是只有第一个值是完全错误的,这意味着我的转换是正确的,但是有一个内存问题,即使有帮助我也无法解决。

代码如下:

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

void * fonction_thread(void * arg);

long s = 0; //int to sum on

int main (int argc, char* argv[])
{
    int N,i ; //N is the number of threads created
    void* ret ;  //returned value

    N= atoi(argv[1]);
    s=0;
    
    for(i=1; i<N+1; i++){

        //creation of the threads
        pthread_t thr;
        if (pthread_create(&thr, NULL, fonction_thread, &i) != 0) {
            fprintf(stderr, "Erreur dans pthread_create\n");
            exit(EXIT_FAILURE);
        }

        //recovering the value
        pthread_join(thr, &ret);
        printf("retour : %d\n\n", *((int*)ret)); //transtyping void* to int* before derefencement
        s+= *((int*)ret);   
    }
    printf("%ld\n", s);
}

//thread handler
void * fonction_thread(void * arg)
{
    int n;
    n = *((int *)arg); //recovering the value given by the main
    printf("thread numéro %d créé\n", n);
    pthread_exit((void*) &n); //casting then returning value
}

这是控制台视图: console screeshot

【问题讨论】:

    标签: c pthreads


    【解决方案1】:

    一个主要问题是你返回一个指向本地变量n的指针。

    fonction_thread 退出后,所有局部变量的生命周期结束,任何指向它们的指针都将失效。

    对于这样的事情,将值转换为指针并再次返回是可以的(几乎没有),这意味着您可以执行以下操作来返回值:

    return (void *) (intptr_t) n;  // Or use pthread_exit((void *) (intptr_t) n)
    

    然后当你收到“指针”时,你会做相反的演员:

    int n = (int) (intptr_t) ret;
    

    请注意,您需要以相同的方式将i 传递给线程。不是因为生命周期问题,而是因为所有线程都将具有完全相同的指针,并且会有数据竞争来获取值,这意味着多个线程似乎获得相同的值。

    所以当你创建线程时传递值:

    pthread_create(&thr, NULL, fonction_thread, (void *) (intptr_t) i)
    

    并在函数中进行相反的强制转换以获得值:

    int n = (int) (intptr_t) arg;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-24
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多