#include #include #include #include int PROCEED = 0; int THREADPRINT = 1; //if 0 main prints, if 1 thread prints. void *thread_method (void *arg); int main (int argc, char **argv) { pthread_t th; void *result; //arguments to and from threads are always void * pointers pthread_create (&th,NULL,thread_method,"I'm running"); //pointer to thread variable,properties of thread (usually NULL),name of method (function pointer),argument to method //pthread_create starts a thread running on the specified method, but main keeps going (normally the main method blocks when it calls another method) /* for (int i=0; i < 10000; i++) { printf ("Main Method\n"); } */ while (THREADPRINT); printf ("Press Enter when you want the other thread to proceed: "); char c[50]; fgets(c,50,stdin); PROCEED = 1; pthread_join (th,&result); //wait for the thread to terminate, and grab what it returns printf ("Thread returns: %s\n",(char *)result); return 0; } void *thread_method (void *arg) { while (!THREADPRINT); printf ("Waiting until I get the signal to proceed\n"); THREADPRINT = 0; while (!PROCEED); char *s = (char *)arg; for (int i=0; i< 10000; i++) { printf ("%s\n",s); } /* int x=5; if (x=0) printf ("Made it here!\n"); */ return "I'm Done!"; }