简体   繁体   中英

pthread_join void **retval

I'm getting a segmentation fault when trying to acess the 4th argument of pthread_join. Here is my code:

void* threadHandler(void* arg)
{
  printf("arg: %c\n", *(char *) arg);
  pthread_exit(0);
}

int main()
{
  pthread_t threadA;
  pthread_create(&threadA, NULL, threadHandler, "N");

  void *retval;
  pthread_join(threadA, &retval);

  printf("retval: %d\n", *(int *) retval);

  return 0;
}

Any idea as to why the last printf causes a segmentation fault error? How do i fix it? (it's my understanding that retval should contain the return value of the threadHandler, so in this case, it should be 0 right?)

Thanks in advance!

If you understand that pthread_join() will pick the return value of the thread callback then you should return from it like this

#include <stdio.h>
#include <stdint.h>
#include <pthread.h>

void *
threadHandler(void *data)
{
    printf("arg: %s\n", (char *) data);
    return (void *) 0;
}

int 
main(void)
{
    pthread_t thread;
    const char *string;
    void *number;

    string = "N";
    if (pthread_create(&thread, NULL, threadHandler, (void *) string) != 0)
        return -1;
    pthread_join(thread, &number);
    printf("%d\n", ((int) (intptr_t) number));
    return 0;
}

Note however, that the returned address 0x00 is probably not a valid address — usually it's the NULL pointer (void *) 0x00 — so you should only do this as long as youre interested in using the value of the pointer as the integer you want to return.

Dereferencing the pointer is Undefined Behavior but, using the value is not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM