#include #include #include #include void* do_the_work (void *data) { int *v = data; int i; for (i = 0; i < 300000000; ++i) { *v = *v + 1; } printf ("done\n"); return NULL; } int main (int argc, char *argv[]) { int v = 0; pthread_t thread; if (argc > 1) { printf ("using a subthread\n"); pthread_create (&thread, NULL, do_the_work, &v); /* the loop above takes more than 1 second on my box, * so this exits before the subthread has finished its work. */ if (strcmp (argv [1], "sleep") == 0) sleep (1); else if (strcmp (argv [1], "join") == 0) pthread_join (thread, NULL); } else { printf ("doing the work in main()\n"); do_the_work (&v); } return 0; }