title: How to use pthreads barrier?
body: <p>Hi Sorry for posting a big dump of code but I'm very new at C code, Basically I'm doing an assignment for college and and I have to implement a "pthread_barrier", now I understand the concept of the barrier (or at least I think I do) But I am just not sure exactly where I  should put it. The assignment states: </p>
  
  <p>"Use pthread_barrier_init and pthread_barrier_wait to ensure that all producer/consumer threads begin producing/consuming at the same time."</p>
  
  <p>This is the extra credit part of the assignment by the way</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  
  #define SIXTY_SECONDS 60000000
  #define ONE_SECOND 1000000
  #define RANGE 10
  #define PERIOD 2
  
  typedef struct {
    int *carpark;
    int capacity;
    int occupied;
    int nextin;
    int nextout;
    int cars_in;
    int cars_out;
    pthread_mutex_t lock;
    pthread_cond_t space;
    pthread_cond_t car;
    pthread_barrier_t bar;
  } cp_t;
  
  /* Our producer threads will each execute this function */
  static void *
  producer(void *cp_in)
  {    
    cp_t *cp;
    unsigned int seed;
    /* Convert what was passed in to a pointer to a bounded buffer */
    cp = (cp_t *)cp_in;
  
      /* Loop */
      while (1) {
      /* Sleep for up to 1s */
      usleep(rand_r(&amp;seed) % ONE_SECOND);
      /* Acquire the lock */
      pthread_mutex_lock(&amp;cp-&gt;lock);
      /* While full wait until there is room available */
      while (cp-&gt;occupied == cp-&gt;capacity) {
        pthread_cond_wait(&amp;cp-&gt;car, &amp;cp-&gt;lock);
      }
      /* Insert an item */
      cp-&gt;carpark[cp-&gt;nextin] = rand_r(&amp;seed) % RANGE;
      /* Increment counters */
      cp-&gt;occupied++;
      cp-&gt;nextin++;
      cp-&gt;nextin %= cp-&gt;capacity;
      cp-&gt;cars_in++;
      /* Someone may be waiting on data to become available */
      pthread_cond_signal(&amp;cp-&gt;space);
      /* Release the lock */
      pthread_mutex_unlock(&amp;cp-&gt;lock);
    }
  
    return ((void *)NULL);
  }
  
  /* Our consumer threads will each execute this function */
  static void *
  consumer(void *cp_in)
  {
  
    cp_t *cp;
    unsigned int seed;
    /* Convert what was passed in to a pointer to a bounded buffer */
    cp = (cp_t *)cp_in;
  
    while (1) {
      /* Sleep for up to 1s */
      usleep(rand_r(&amp;seed) % ONE_SECOND);
      /* Acquire the lock */
      pthread_mutex_lock(&amp;cp-&gt;lock);
      /* While empty wait until there is data available */
  
      while (cp-&gt;occupied == 0) {
        pthread_cond_wait(&amp;cp-&gt;space, &amp;cp-&gt;lock);
      }
  
      /* Increment counters */
      cp-&gt;occupied--;
      cp-&gt;nextout++;
      cp-&gt;nextout %= cp-&gt;capacity;
      cp-&gt;cars_out++;
      /* Someone may be waiting on room to become available */
      pthread_cond_signal(&amp;cp-&gt;car);
      /* Release the lock */
      pthread_mutex_unlock(&amp;cp-&gt;lock);
    }
  
    return ((void *)NULL);
  }
  
  /* Our monitor thread will each execute this function */
  static void *
  monitor(void *cp_in)
  {
  
    cp_t *cp;
    /* Convert what was passed in to a pointer to a bounded buffer */
    cp = (cp_t *)cp_in;
  
    while (1) {
      /* Pause */
      sleep(PERIOD);
      /* Acquire the lock */
      pthread_mutex_lock(&amp;cp-&gt;lock);
      printf("Delta: %d\n", cp-&gt;cars_in - cp-&gt;cars_out);
      /* Release the lock */
      pthread_mutex_unlock(&amp;cp-&gt;lock);
    }
  
    return ((void *)NULL);
  }
  
  /* Initialisation */
  static int
  init(cp_t *cp, int capacity)
  {
  
    /* Set up the bounded buffer internals */
    cp-&gt;occupied = cp-&gt;nextin = cp-&gt;nextout = cp-&gt;cars_in = cp-&gt;cars_out = 0;
    cp-&gt;capacity = capacity;
    /* Initialise our data structure */
    cp-&gt;carpark = (int *)malloc(cp-&gt;capacity * sizeof (*cp-&gt;carpark));
    /* Check malloc succeeded */
  
    if (cp-&gt;carpark == NULL) {
      perror("malloc()");
      exit(EXIT_FAILURE);
    }
  
    /* Initialise lock and condition variables */
    pthread_mutex_init(&amp;cp-&gt;lock, NULL);
    pthread_cond_init(&amp;cp-&gt;space, NULL);
    pthread_cond_init(&amp;cp-&gt;car, NULL);  
    /* Seed random number generator */
    srand((unsigned int)getpid());
  
    return (0);
  }
  
  int
  main(int argc, char *argv[])
  {
  
    pthread_t p, c, m;
    cp_t cp;
    /* Check usage */
  
    if (argc != 2) {
      printf("Usage: %s buffer_size\n", argv[0]);
      exit(EXIT_FAILURE);
    }
  
    /* Initialise */
    init(&amp;cp, atoi(argv[1]));
    /* Create our threads */
    pthread_create(&amp;p, NULL, producer, (void *)&amp;cp);
    pthread_create(&amp;p, NULL, producer, (void *)&amp;cp);
    pthread_create(&amp;c, NULL, consumer, (void *)&amp;cp);
    pthread_create(&amp;c, NULL, consumer, (void *)&amp;cp);
    pthread_create(&amp;m, NULL, monitor, (void *)&amp;cp);
    /* Wait for our threads */
    pthread_join(p, NULL);
    pthread_join(p, NULL);
    pthread_join(c, NULL);
    pthread_join(c, NULL);
    pthread_join(m, NULL);
  
    return (0);
  }
  </code></pre>