Created
July 26, 2019 00:29
-
-
Save surinoel/97a1e3541162b0ef29fc18cbf8eb75da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
void *thread_main(void *arg) { | |
for(int i=0; i<5; i++) { | |
printf("Running time : %d\n", i+1); | |
sleep(1); | |
} | |
printf("thread main exit\n"); | |
return NULL; | |
} | |
int main(int argc, char **argv) | |
{ | |
pthread_t tid; | |
pthread_attr_t attr; | |
int state; | |
if(pthread_attr_init(&attr) != 0) { | |
return -1; | |
} | |
if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) { | |
return -1; | |
} | |
if(pthread_attr_getdetachstate(&attr, &state) != 0) { | |
return -1; | |
} | |
printf("pthread_attr_getdetachstate : %d\n", state); | |
if(pthread_create(&tid, &attr, thread_main, NULL) != 0) { | |
return -1; | |
} | |
if(pthread_attr_destroy(&attr) != 0) { | |
return -1; | |
} | |
while(1) { | |
sleep(1); | |
printf("hello world\n"); | |
} | |
return 0; | |
} | |
#if 0 | |
pthread_attr_getdetachstate : 1 | |
Running time : 1 | |
hello world | |
Running time : 2 | |
hello world | |
Running time : 3 | |
hello world | |
Running time : 4 | |
hello world | |
Running time : 5 | |
hello world | |
thread main exit | |
hello world | |
hello world | |
hello world | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment