Error when returning multiple values to threads in C

2

I set up several threads, each of them would be filled with multiple values and it should make query to DB and return multiple values.

The single thread can display the correct values, but something went wrong when they return values to main function.

The main function is:

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);
}

The single thread:

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;
}

The result was as following:

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.

It seems works well if it is compiled by mingw64 on win10, but somehow I have to compile it in 32 bit. My IDE is Codeblocks, differs from mingw64, I had to add a linker MinGW\lib\libpthread_s.dll.a and MinGW\lib\libpthread.a there when I compile it with mingw32. I don't know if it could be the problem? and how could I fix it?

c
multithreading
pthreads
mingw
mingw32
asked on Stack Overflow Jan 22, 2020 by heisthere • edited Jan 23, 2020 by heisthere

2 Answers

0

You need to be more careful about the use of pthread_join():

  • first, before using the return value, you need to check if the function succeeded. Otherwise your return value will not be what you expect.
  • you also should initialize your pointer for the retvalue.
answered on Stack Overflow Jan 22, 2020 by Christophe
0

The way I return the values was actually not a problem. That was problem at the libraries I used. So I use Mingw installation manager (which should be installed by default) to reinstall the min3gw2-libpthread... libraries. and it worked.

answered on Stack Overflow Jan 23, 2020 by heisthere

User contributions licensed under CC BY-SA 3.0