Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 26, 2019 00:29
Show Gist options
  • Save surinoel/97a1e3541162b0ef29fc18cbf8eb75da to your computer and use it in GitHub Desktop.
Save surinoel/97a1e3541162b0ef29fc18cbf8eb75da to your computer and use it in GitHub Desktop.
#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