pthread_detach
- 功能:线程游离
- 原型:
int pthread_detach(pthread_t thread);
- 参数:
1)tid - 返回值:
成功:返回0
失败:返回错误码,不会设置errno - DO
#include <func.h>
typedef struct {
int id;
char name[25];
char gender;
int chinese;
int math;
int english;
} Student;
void print_stu_info(Student* s) {
printf("%d %s %c %d %d %d\n",
s->id,
s->name,
s->gender,
s->chinese,
s->math,
s->english);
}
void* start_routine(void* args) {
Student* s = (Student*) malloc(sizeof(Student));
s->id = 1;
strcpy(s->name, "xixi");
s->gender = 'f';
s->chinese = 100;
s->math = 100;
s->english = 100;
return (void*) s;
}
int main(int argc, char* argv[])
{
pthread_t tid1;
int err = pthread_create(&tid1, NULL, start_routine, NULL);
if (err) {
error(1, err, "pthread_create");
}
err = pthread_detach(tid1);
if (err) {
error(1, err, "pthread_detach %lu", tid1);
}
Student* s1;
err = pthread_join(tid1, (void**)&s1);
if (err) {
error(1, err, "pthread_join %lu", tid1);
}
print_stu_info(s1);
free(s1);
return 0;
}