Created
April 2, 2012 20:29
-
-
Save aelipek/2287016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
pthread_mutex_tmutex=PTHREAD_MUTEX_INITILIZER; | |
intshared_data=1; | |
void *consumer(void* arg) { | |
for(intI =0; I < 30 ; I ++ ){ | |
pthread_mutex_lock( &mutex); | |
shared_data–; /* Critical Section. */ | |
pthread_mutex_unlock( &mutex); | |
} | |
printf(“Returning from Comsumer=%d\n”, shared_data); | |
} | |
void main() { | |
pthread_tthread_id; | |
pthread_create( & thread_id, NULL, consumer, NULL ); | |
for(intI =0; I < 30 ; I ++ ){ | |
pthread_mutex_lock( &mutex); | |
shared_data++; /* Producer Critical Section. */ | |
pthread_mutex_unlock( &mutex); | |
} /*pthread_exit(0); /* Return from main thread. */ | |
printf(“End of main =%d\n”, shared_data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment