c - Thread created (detached) never executed -
i have written code:
void* th (void* arg) { sleep(1); for(int i=0; i<1000;i++) { fprintf(stderr,"%d\t",i); } pthread_exit(null); } int main(int argc, char** argv) { pthread_t thread; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,pthread_create_detached); pthread_create(&thread,&attr,th,null); pthread_attr_destroy(&attr); return 0; }
the detach state should make thread not joinable, should run after main process terminated.but doesn't print numbers, see thread terminated without printing stderr.
why isn't detached thread executed?
a return
main
thread equivalent exit
of whole process, process exit before thread may print anything. use pthread_exit
instead terminate thread.
Comments
Post a Comment