-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-3detach.c
39 lines (38 loc) · 856 Bytes
/
4-3detach.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <pthread.h>
#define errlog(errmsg) do{perror(errmsg);\
printf("--%s--%s--%d--\n",\
__FILE__, __FUNCTION__, __LINE__);\
return -1;\
}while(0)
void *thread_handler(void *arg){
printf("thread...\n");
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t thread;
pthread_attr_t attr;
if(pthread_attr_init(&attr) != 0){
errlog("pthread_attr_init error");
}
if(pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_DETACHED) != 0){
errlog("pthread_attr_setdetachstate error");
}
if(pthread_create(&thread, &attr,
thread_handler, NULL) != 0){
errlog("pthread_create1 error");
}
int temp = 0;
sleep(1);
if(pthread_join(thread, NULL) == 0){
printf("pthread wait success\n");
temp = 0;
}
else{
printf("pthread wait failed\n");
temp = 0;
}
return temp;
}