forked from linw7/Skill-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread_detach.cpp
More file actions
38 lines (36 loc) · 774 Bytes
/
pthread_detach.cpp
File metadata and controls
38 lines (36 loc) · 774 Bytes
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
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void *tfn1(void *args){
printf("the sub thread sleeping 5 seconds \n");
sleep(5);
printf("the thread done \n");
return NULL;
}
int main() {
int iRet;
pthread_t tid;
pthread_attr_t attr;
iRet = pthread_create(&tid, NULL, tfn1, NULL);
if(iRet){
printf("can't create thread %s \n", strerror(iRet));
return iRet;
}
iRet = pthread_detach(tid);
if(iRet){
printf("can't detach thread %s \n", strerror(iRet));
return iRet;
}
printf("hello\n");
iRet = pthread_join(tid, NULL);
if(iRet){
printf("pthread has been detached \n");
return iRet;
}
printf("the main thread sleeping 8 seconds \n");
sleep(8);
printf("the main thread done \n");
return 0;
}