1

I can not figure out where I'm wrong, after running the code arrived in the for where it runs the pthread_join() , many pthread_join() return with value 3 instead of 0. Furthermore, printing the value of i is not always consistent and this causes segmentation fault and printing several times of the same position. Code modified as required in the comments all the includes are for other parts of the program. Testing only this piece of code creates segmentation fault at error 3 on pthread_join()

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>

#include <errno.h>
#include <config.h>
#include <sys/select.h>
#include <ctype.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <sys/un.h>

void *threadF(){
    printf("hello\n");
    pthread_exit((void*)0);     
}




int main(int argc, char *argv[]) {

    FILE *fileconf=fopen(argv[2],"r"); 
    if(fileconf==NULL){
        fprintf(stderr, "Fopen\n",argv[2]);
        return -1;
    }
    set_conf(fileconf); //parse fileconf and set THREADSINPOOL correctly

    pthread_t array[THREADSINPOOL];
    int i,err,s=0;
    for(i=0;i<THREADSINPOOL;i++){
        if((err=pthread_create(&array[i],NULL,&threadF,NULL))!=0){
            fprintf(stderr,"thread\n");
            exit(errno);
        }
    }

    int tmp;

    for(i=0;i<THREADSINPOOL;i++){
        tmp=pthread_join(array[i],(void *)&s);
        printf("thread: %lu terminated\n tmp: %d\n",array[i],tmp);
    }

    return 0;
}
JayJona
  • 469
  • 1
  • 16
  • 41
  • A [mcve] is needed. The symptoms you describe indicate some sort of undefined behavior. We won't be able to see that without a complete set of code (by which I mean a set of code that can be compiled and run, but as short as possible). – user3386109 Aug 15 '18 at 17:43
  • 3
    First thing is to understand what `3` means. This is where the `man` pages are helping. – Eugene Sh. Aug 15 '18 at 17:43
  • Code edited, this simple code causes seg fault – JayJona Aug 15 '18 at 18:01
  • Your `printf()` format is expecting two values but you're only passing 1, which causes undefined behavior and all bets are off... if your compiler isn't warning you about this, remember to always compile with `-Wall -Wextra` at a minimum on gcc and clang. – Shawn Aug 15 '18 at 18:01
  • I pass 2 values to the printf, maybe you had seen the old code – JayJona Aug 15 '18 at 18:05
  • 1
    `pthread_join` is taking `void **` and not `void *`. And apparently `int s` might not have space to hold `void*`. – Eugene Sh. Aug 15 '18 at 18:12
  • Your thread function has the wrong interface, your compiler could have warned you. If you use such a wrong function, all sort of things can go wrong, including returning you bogus values. Read the manual and use the interface as it is decribed there, even if you don't need the function parameter for your small example. You should probably not use `thread_exit` there, either. The normal return from a thread function is ... `return`. Also `0` is compatible with all object pointers, no need for a cast. – Jens Gustedt Aug 15 '18 at 18:41

1 Answers1

0

The problem is that you are passing the address of an int to a function that expects the address of a void *. On a 64-bit system, there's a good chance that an int is only 32-bits whereas a void * is 64-bits. So pthread_join ends up writing 64-bits into a location that is only big enough for 32-bits. The result is that you overwrite memory that shouldn't being changed, and all sorts of undefined behavior follows.

Here's a way to write the code so that the second argument to pthread_join is actually a pointer to a void *

for (i = 0; i < THREADSINPOOL; i++)
{
    void *value;
    if (pthread_join(array[i], &value) == 0)
        printf("thread %d returned %" PRIiPTR "\n", i, (intptr_t)value);
    else
        printf("thread %d failed\n", i);
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • this code works if you don't use the printf to print value >cast from pointer to integer of different size [-Werror=pointer-to-int-cast] printf("thread %d returned %d\n", i, (int)value); – JayJona Aug 16 '18 at 16:04