Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the pthreads functions set the errno internally?

Tags:

c

linux

pthreads

In the below code i am calling pthread_join() with thread id as self. Result is that it returns error number 35. So same i am trying to print with perror. But it is displaying "success". My doubt is does library/system calls need to explictly set the errno for any errors or Did i miss any thing ?

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

#define DEATH(mess) { perror(mess); exit(errno); }

static void * threadFunc(void *arg)
{
 void *res;
 printf("sleeping for 2 sec ...\n");
 sleep(2);
 char *s = (char *) arg;
 pthread_t t = pthread_self();
 int relval = pthread_join(t, &res);

 if (relval) 
  perror("deadlock");

  printf("return value is %d .....\n",relval);
  return (void *) strlen(s);
}

int main(int argc, char *argv[])
{
 pthread_t t1;
 void *res;
 int ret;

 ret = pthread_create(&t1, NULL, threadFunc, "Hello world\n");
 if (ret != 0)
    DEATH ("pthread_create");

 printf("Message from main()\n");

 pthread_exit(&res);
 exit(EXIT_SUCCESS);

}

o/p

Message from main()
sleeping for 2 sec ...
deadlock: Success
return value is 35 .....
like image 637
Gowtam Avatar asked Dec 16 '22 14:12

Gowtam


2 Answers

There is no requirement that thesr functions set errno or that they leave it alone. You're always free to do:

errno = pthread_join(t, &res);
like image 164
R.. GitHub STOP HELPING ICE Avatar answered Jan 29 '23 02:01

R.. GitHub STOP HELPING ICE


pthread_join() does not set the errno value

Please refer to the discussion here:

pthread: join a detached thread doesn't set errno correctly

Your best bet is to just use the return value of pthread_join

like image 43
Forhad Ahmed Avatar answered Jan 29 '23 04:01

Forhad Ahmed