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 .....
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With