#ifndef __IO_H__
#define __IO_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#endif
#include "io.h"
void* thread_fun(void* arg)
{
int i = 0;
while (i++ < 10)
{
printf("i=%d\n", i);
sleep(1);
}
pthread_exit((void*)0);
}
int main(int argc, char const *argv[])
{
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&th, &attr, thread_fun, NULL);
/*pthread_attr_destroy(&attr);*/
int state;
pthread_attr_getdetachstate(&attr, &state);
if (state == PTHREAD_CREATE_DETACHED) {
printf("set detach ok!\n");
} else {
printf("set detach error!\n");
}
if(pthread_join(th, NULL)){
fprintf(stderr, "join:success!%s\n", strerror(errno));
}
pthread_attr_destroy(&attr);
return 0;
}
线程属性-分离
最新推荐文章于 2024-03-23 21:43:13 发布