Back to index

glibc  2.9
Classes | Defines | Functions
ex2.c File Reference
#include <stdio.h>
#include "pthread.h"

Go to the source code of this file.

Classes

struct  prodcons

Defines

#define BUFFER_SIZE   16
#define OVER   (-1)

Functions

static void init (struct prodcons *b)
static void put (struct prodcons *b, int data)
static int get (struct prodcons *b)
static void * producer (void *data)
static void * consumer (void *data)
int main (void)

Class Documentation

struct prodcons

Definition at line 13 of file ex2.c.

Collaboration diagram for prodcons:
Class Members
int buffer
pthread_mutex_t lock
pthread_cond_t notempty
pthread_cond_t notfull
int readpos
sem_t sem_read
sem_t sem_write
int writepos

Define Documentation

#define BUFFER_SIZE   16

Definition at line 9 of file ex2.c.

#define OVER   (-1)

Definition at line 79 of file ex2.c.


Function Documentation

static void* consumer ( void *  data) [static]

Definition at line 97 of file ex2.c.

{
  int d;
  while (1)
    {
      d = get (&buffer);
      if (d == OVER)
       break;
      printf ("---> %d\n", d);
    }
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int get ( struct prodcons b) [static]

Definition at line 56 of file ex2.c.

{
  int data;
  pthread_mutex_lock (&b->lock);
  /* Wait until buffer is not empty */
  while (b->writepos == b->readpos)
    {
      pthread_cond_wait (&b->notempty, &b->lock);
    }
  /* Read the data and advance read pointer */
  data = b->buffer[b->readpos];
  b->readpos++;
  if (b->readpos >= BUFFER_SIZE)
    b->readpos = 0;
  /* Signal that the buffer is now not full */
  pthread_cond_signal (&b->notfull);
  pthread_mutex_unlock (&b->lock);
  return data;
}

Here is the call graph for this function:

static void init ( struct prodcons b) [static]

Definition at line 24 of file ex2.c.

Here is the call graph for this function:

int main ( void  )

Definition at line 111 of file ex2.c.

{
  pthread_t th_a, th_b;
  void *retval;

  init (&buffer);
  /* Create the threads */
  pthread_create (&th_a, NULL, producer, 0);
  pthread_create (&th_b, NULL, consumer, 0);
  /* Wait until producer and consumer finish. */
  pthread_join (th_a, &retval);
  pthread_join (th_b, &retval);
  return 0;
}

Here is the call graph for this function:

static void* producer ( void *  data) [static]

Definition at line 84 of file ex2.c.

{
  int n;
  for (n = 0; n < 10000; n++)
    {
      printf ("%d --->\n", n);
      put (&buffer, n);
    }
  put (&buffer, OVER);
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void put ( struct prodcons b,
int  data 
) [static]

Definition at line 35 of file ex2.c.

{
  pthread_mutex_lock (&b->lock);
  /* Wait until buffer is not full */
  while ((b->writepos + 1) % BUFFER_SIZE == b->readpos)
    {
      pthread_cond_wait (&b->notfull, &b->lock);
      /* pthread_cond_wait reacquired b->lock before returning */
    }
  /* Write the data and advance write pointer */
  b->buffer[b->writepos] = data;
  b->writepos++;
  if (b->writepos >= BUFFER_SIZE)
    b->writepos = 0;
  /* Signal that the buffer is now not empty */
  pthread_cond_signal (&b->notempty);
  pthread_mutex_unlock (&b->lock);
}

Here is the call graph for this function:

Here is the caller graph for this function: