Tuesday, April 19, 2011

pthread example - producer/consumer

Another simple pthread example. It implements a producer/consumer pattern. Threads are created by calling pthread_create() function. pthread_join() just waits for the specified thread to finish. In this pthread example, one thread produces data and another consumes it. The pthread example is shown below.


// pthread example - producer-consumer pattern.
//
#include <sys/time.h>
#include <stdio.h>
#include <pthread.h> 
#include <errno.h>

pthread_mutex_t region_mutex = PTHREAD_MUTEX_INITIALIZER;

int b;  /* buffer size = 1; */ 
main()  
{
  pthread_t producer_thread; 
  pthread_t consumer_thread; 
  void *producer();
  void *consumer();
  
    pthread_create(&consumer_thread,NULL,consumer,NULL);
    pthread_create(&producer_thread,NULL,producer,NULL);
    pthread_join(consumer_thread,NULL);
}

void add_buffer(int i){
  b = i;
}
int get_buffer(){
  return b ;
}
 
void *producer()
{
int i = 0;
printf("I'm a producer\n");
while (1) {
  pthread_mutex_lock(&region_mutex);
  add_buffer(i);
  pthread_mutex_unlock(&region_mutex);
  i = i + 1;
}
pthread_exit(NULL);
}
void *consumer()
{
int i,v;
printf("I'm a consumer\n");
for (i=0;i<100;i++) {
   pthread_mutex_lock(&region_mutex);
   v = get_buffer();
   pthread_mutex_unlock(&region_mutex);
   printf("got %d  ",v);
}
pthread_exit(NULL);
}