FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
October 01, 2004

The FifoEmbed Library

(Page 10 of 12)

October, 2004: The FifoEmbed Library

Listing 5

#include <pthread.h>
#include <semaphore.h>

#define FIFO_CELL_T int
#define BASIC_FIFO_T int_queue
#include "fifo.h"

int_queue q;

sem_t q_count;
BOOL more_room;
pthread_cond_t more_room_cv;
pthread_mutex_t more_room_lock;

int get_data () { /* ... */ }
void process_data (int x) { /* ... */ }

void producer () {
  for (;;) {
    int x = get_data ();
    pthread_mutex_lock (& more_room_lock);
    for (;;) {
      more_room = FALSE;
      pthread_mutex_unlock (& more_room_lock);
      if (FIFO_INS_CHK (& q, x)) break;
      pthread_mutex_lock (& more_room_lock);
      while (! more_room)
        pthread_cond_wait (& more_room_cv, & more_room_lock);
    }
    sem_post (& q_count);
  }
}

void consumer () {
  for (;;) {
    sem_wait (& q_count);
    process_data (FIFO_READ (& q));
    pthread_mutex_lock (& more_room_lock);
    more_room = TRUE;
    pthread_mutex_unlock (& more_room_lock);
    pthread_cond_signal (& more_room_cv);
  }
}

Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK