在下面的代码中,我调用 pthread_join(),线程 id 为 self。结果是它返回错误号 35。所以我也试图用 perror 打印。但它正在显示“成功”。我的疑问是库/系统调用是否需要为任何错误明确设置 errno 或者我错过了什么?
#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 .....