/* * file thread.c * compiling: gcc -o thread thread.c -lpthread */ #include <stdio.h> #include <pthread.h> /* * Arguments to print_arg */ struct arg { char * str; int siz; }; /* * thread function */ void * print_arg (void * arguments) { struct arg * ptr = (struct arg *) arguments; write (1, ptr->str, ptr->siz); } int main (void) { struct arg args = { "Hello World\n", 12, }; /* * thread atribute */ pthread_attr_t thread_attr; /* * thread id */ pthread_t thread; /* * init, set to detach, create it */ pthread_attr_init (&thread_attr); pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED); pthread_create (&thread, &thread_attr, &print_arg, &args); /* * destroy, this don't deallocate things */ pthread_attr_destroy (&thread_attr); return 0; }Detached thread don't need to return the value to main..
so thread_join isn't needed here... but..
if main return before thread, then thread
is interrupted.. this is what people call bug
sometime works .. sometime not
Nenhum comentário:
Postar um comentário