c - 将多个值返回到C中的线程时出错

标签 c multithreading pthreads mingw mingw32

我设置了几个线程,每个线程都将填充多个值,并且应该向数据库查询并返回多个值。

单线程可以显示正确的值,但是当它们将值返回给main函数时出了点问题。

主要功能是:

for(int i=0; i<n; i++){
    struct thread_args* args = malloc(sizeof(*args));
    args->str = str;
    args->tri_input = tri_input;
    args->length = length;
    res = pthread_create(&t_id[i], NULL, threads, args);
    if(res != 0)
        printf("Failed to create %d th thread.\n", i);
}

for(int i=0; i<n; i++){
    void *returnRes;
    pthread_join(t_id[i], &returnRes);
    indicesArr[i] = returnRes;
    printf("in main thread, indicesArr[%d].length: %d\n", i, indicesArr[i] -> length);
}


单线程:
void *threads(void *args_){

    struct thread_args *args = (struct thread_args *)args_;
    char* str = args -> str;
    struct IntArrLenArr* indicesArr = NULL;
    indicesArr = malloc(sizeof(*indicesArr));
    int length = args -> length;
    PGconn *dbconn = DBconnect(str);
    *indicesArr = duncitonReturnIndicesArr(dbconn);
    printf("in threads, indicesArr -> length: %d\n", indicesArr->length);
    free(args);
    PQfinish(dbconn);
    return indicesArr;
}


结果如下:
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 0
in main threads, indicesArr[0].length: -1949512500
in threads, indicesArr -> length: 2
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 1
in threads, indicesArr -> length: 4

Process returned -1073741819 (0xC0000005)   execution time : 6.551 s
Press any key to continue.

如果它是由mingw64在win10上编译的,则似乎效果很好,但是我必须以某种方式将其编译为32位。我的IDE是Codeblocks,与mingw64不同,我在用mingw32进行编译时必须在其中添加链接器MinGW\lib\libpthread_s.dll.aMinGW\lib\libpthread.a。我不知道这可能是问题吗?我该如何解决?

最佳答案

我返回值的方式实际上不是问题。那是我使用的库的问题。
因此,我使用Mingw安装管理器(应默认安装)重新安装min3gw2-libpthread...库。而且有效。

关于c - 将多个值返回到C中的线程时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59861537/

相关文章:

c - 如何计算单词列表中单词的出现次数

c++ - 等待 QTcpSocket 数据时阻止 GUI

c# - 如何在 C# 中的指定时间后取消后台工作程序

C Pthread : Kill other threads gracefully through one thread

c - 使 OpenMP 程序与 Pthreads 一起工作,出现段错误

c++ - 解除线程与另一个线程的阻塞

c - GTK:在滚动窗口滚动后获取滚动事件的指针位置

c - gcc -c 参数出现问题并设置输出文件

c++ - 为什么我们在 Boost.ASIO 中需要很多接受器?

c - 矩阵乘法中的段错误