ch4-detach

Chapter_4     primes critical-section







create_one.c     ALP, p. 64, 69


#include <stdio.h> // fputc(), stderr(), NULL
#include <pthread.h> // for pthread_t, pthread_attr_t, pthread_attr_setdetachstate,
// PTHREAD_CREATE_DETACHED, pthread_create(), pthread_attr_destroy()
#include <unistd.h> // for sleep()

void * print_xs (void * unused) // The parameter is unused
{
while(1) // for ever
{
fputc ('x', stderr); // print x's to stderr
sleep(1); // pause for 1 second
}

return NULL; // Does not return
}

int main()
{
pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
/* Create a new thread to will run the print_xs function: */
pthread_create (&thread_id, &attr, &print_xs, NULL);
pthread_attr_destroy (&attr);

while(1) /* Print o's continuously to stderr */
{
fputc ('o', stderr);
sleep(1); // pause for 1 second
}

return 0;
}

/*
gcc create_one.c -o create_one -lpthread
./create_one
oxoxoxxoxoxoxooxxoxooxoxoxxooxxoxooxoxoxoxxooxoxoxoxoxoxoxoxoxoxox^C
// ended with Ctrl^C
*/





Note:  Compare to create1 above.









Chapter_4     primes BACK_TO_TOP critical-section



Comments

Popular posts from this blog

Contents