Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 0.61 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Producer and consumer always skips the same number
  2. sem_init(&empty, 0, SIZE-1);
  3. sem_init(&full, 0, 0);
  4.        
  5. sem_wait(&empty);
  6.  
  7. pthread_mutex_lock(&write);
  8. //produce
  9. pthread_mutex_unlock(&write);
  10.  
  11. sem_post(&full);
  12.        
  13. sem_wait(&full);
  14.  
  15. pthread_mutex_lock(&readers);
  16. readCounter += 1;
  17. if (readCounter != 3)
  18.     sem_post(&full);
  19. pthread_mutex_unlock(&readers);
  20.  
  21. // read
  22.  
  23. pthread_mutex_lock(&readers);
  24. readCounter -= 1;
  25. if(readCounter == 0)
  26. {
  27.     sem_post(&empty);
  28. }
  29. pthread_mutex_unlock(&readers);
  30.        
  31. sem_wait(&full);
  32.  
  33. pthread_mutex_lock(&readers);
  34. // read
  35. pthread_mutex_unlock(&readers);
  36.  
  37. // process item
  38.  
  39. sem_post(&empty);