Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #define BUF_SIZE 5
  7.  
  8. // the buffer works like a stack for
  9. // the sake of simplicity, if needed
  10. // we may implement a queue
  11. typedef struct {
  12. int buf[BUF_SIZE]; // the buffer
  13. size_t len; // number of items in the buffer
  14. pthread_mutex_t mutex; // needed to add/remove data from the buffer
  15. pthread_cond_t can_produce; // signaled when items are removed
  16. pthread_cond_t can_consume; // signaled when items are added
  17. } buffer_t;
  18.  
  19. // produce random numbers
  20. void* producer(void *arg) {
  21. buffer_t *buffer = (buffer_t*)arg;
  22.  
  23. while(1) {
  24. #ifdef UNDERFLOW
  25. // used to show that if the producer is somewhat "slow"
  26. // the consumer will not fail (i.e. it'll just wait
  27. // for new items to consume)
  28. sleep(rand() % 3);
  29. #endif
  30.  
  31. pthread_mutex_lock(&buffer->mutex);
  32.  
  33. if(buffer->len == BUF_SIZE) { // full
  34. // wait until some elements are consumed
  35. pthread_cond_wait(&buffer->can_produce, &buffer->mutex);
  36. }
  37.  
  38. // in real life it may be some data fetched from
  39. // sensors, the web, or just some I/O
  40. int t = rand();
  41. printf("Produced: %dn", t);
  42.  
  43. // append data to the buffer
  44. buffer->buf[buffer->len] = t;
  45. ++buffer->len;
  46.  
  47. // signal the fact that new items may be consumed
  48. pthread_cond_signal(&buffer->can_consume);
  49. pthread_mutex_unlock(&buffer->mutex);
  50. }
  51.  
  52. // never reached
  53. return NULL;
  54. }
  55.  
  56. // consume random numbers
  57. void* consumer(void *arg) {
  58. buffer_t *buffer = (buffer_t*)arg;
  59.  
  60. while(1) {
  61. #ifdef OVERFLOW
  62. // show that the buffer won't overflow if the consumer
  63. // is slow (i.e. the producer will wait)
  64. sleep(rand() % 3);
  65. #endif
  66. pthread_mutex_lock(&buffer->mutex);
  67.  
  68. if(buffer->len == 0) { // empty
  69. // wait for new items to be appended to the buffer
  70. pthread_cond_wait(&buffer->can_consume, &buffer->mutex);
  71. }
  72.  
  73. // grab data
  74. --buffer->len;
  75. printf("Consumed: %dn", buffer->buf[buffer->len]);
  76.  
  77. // signal the fact that new items may be produced
  78. pthread_cond_signal(&buffer->can_produce);
  79. pthread_mutex_unlock(&buffer->mutex);
  80. }
  81.  
  82. // never reached
  83. return NULL;
  84. }
  85.  
  86. int main(int argc, char *argv[]) {
  87. buffer_t buffer = {
  88. .len = 0,
  89. .mutex = PTHREAD_MUTEX_INITIALIZER,
  90. .can_produce = PTHREAD_COND_INITIALIZER,
  91. .can_consume = PTHREAD_COND_INITIALIZER
  92. };
  93.  
  94. pthread_t prod, cons;
  95. pthread_create(&prod, NULL, producer, (void*)&buffer);
  96. pthread_create(&cons, NULL, consumer, (void*)&buffer);
  97.  
  98. pthread_join(prod, NULL); // will wait forever
  99. pthread_join(cons, NULL);
  100.  
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement