Skip to content

Instantly share code, notes, and snippets.

@aelipek
Created April 2, 2012 20:29
Show Gist options
  • Save aelipek/2287016 to your computer and use it in GitHub Desktop.
Save aelipek/2287016 to your computer and use it in GitHub Desktop.
#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