在下面的代码中,我使用自身作为线程 ID 调用 pthread_join()。结果是返回错误号 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 .....