/* * CSE451 Section Pthread Example Code * April 16, 2009 */ #include #include #include #include #define MAXCOUNT 500 #define SIGNALCOUNT 3 void *count_up(void *args); void *subtract(void *args); pthread_mutex_t *mutex; pthread_cond_t *done; int main () { //thread handles pthread_t cu, s; //shared variable between the two threads int shared_int = 0; //allocate and init the mutex mutex = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t)); if(!mutex) { exit(1); } pthread_mutex_init (mutex, NULL); //allocate and init the condition variable done = (pthread_cond_t *) malloc (sizeof (pthread_cond_t)); if(!done) { exit(1); } pthread_cond_init (done, NULL); //do work son pthread_create (&cu, NULL, count_up, &shared_int); pthread_create (&s, NULL, subtract, &shared_int); //wait for threads to finish pthread_join (cu, NULL); pthread_join (s, NULL); //clean up pthread_mutex_destroy(mutex); pthread_cond_destroy(done); return 0; } void *count_up (void *args) { int i; int *count = (int*)args; for(i=0;i