ch4-create1
Chapter_4 | create2 |
create1.c ALP, p. 64
#include <stdio.h> // fputc(), stderr(), NULL
#include <pthread.h> // for pthread_t, pthread_create()
#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;
/* Create a new thread. The new thread will run the print_xs
function. */
pthread_create (&thread_id, NULL, &print_xs, NULL);
while(1) /* Print o's continuously to stderr */
{
fputc ('o', stderr);
sleep(1); // pause for 1 second
}
return 0;
}
/*
gcc create1.c -o create1 -lpthread
./create1
oxoxoxoxoxxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxooxoxox^C
// ended with Ctrl^C
*/
Chapter_4 | BACK_TO_TOP | create2 |
Comments
Post a Comment