lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* The classic producer-consumer example. |
| 2 | Illustrates mutexes and conditions. |
| 3 | All integers between 0 and 9999 should be printed exactly twice, |
| 4 | once to the right of the arrow and once to the left. */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include "pthread.h" |
| 8 | |
| 9 | #define BUFFER_SIZE 16 |
| 10 | |
| 11 | /* Circular buffer of integers. */ |
| 12 | |
| 13 | struct prodcons { |
| 14 | int buffer[BUFFER_SIZE]; /* the actual data */ |
| 15 | pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */ |
| 16 | int readpos, writepos; /* positions for reading and writing */ |
| 17 | pthread_cond_t notempty; /* signaled when buffer is not empty */ |
| 18 | pthread_cond_t notfull; /* signaled when buffer is not full */ |
| 19 | }; |
| 20 | |
| 21 | /* Initialize a buffer */ |
| 22 | |
| 23 | static void init(struct prodcons * b) |
| 24 | { |
| 25 | pthread_mutex_init(&b->lock, NULL); |
| 26 | pthread_cond_init(&b->notempty, NULL); |
| 27 | pthread_cond_init(&b->notfull, NULL); |
| 28 | b->readpos = 0; |
| 29 | b->writepos = 0; |
| 30 | } |
| 31 | |
| 32 | /* Store an integer in the buffer */ |
| 33 | |
| 34 | static void put(struct prodcons * b, int data) |
| 35 | { |
| 36 | pthread_mutex_lock(&b->lock); |
| 37 | /* Wait until buffer is not full */ |
| 38 | while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) { |
| 39 | pthread_cond_wait(&b->notfull, &b->lock); |
| 40 | /* pthread_cond_wait reacquired b->lock before returning */ |
| 41 | } |
| 42 | /* Write the data and advance write pointer */ |
| 43 | b->buffer[b->writepos] = data; |
| 44 | b->writepos++; |
| 45 | if (b->writepos >= BUFFER_SIZE) b->writepos = 0; |
| 46 | /* Signal that the buffer is now not empty */ |
| 47 | pthread_cond_signal(&b->notempty); |
| 48 | pthread_mutex_unlock(&b->lock); |
| 49 | } |
| 50 | |
| 51 | /* Read and remove an integer from the buffer */ |
| 52 | |
| 53 | static int get(struct prodcons * b) |
| 54 | { |
| 55 | int data; |
| 56 | pthread_mutex_lock(&b->lock); |
| 57 | /* Wait until buffer is not empty */ |
| 58 | while (b->writepos == b->readpos) { |
| 59 | pthread_cond_wait(&b->notempty, &b->lock); |
| 60 | } |
| 61 | /* Read the data and advance read pointer */ |
| 62 | data = b->buffer[b->readpos]; |
| 63 | b->readpos++; |
| 64 | if (b->readpos >= BUFFER_SIZE) b->readpos = 0; |
| 65 | /* Signal that the buffer is now not full */ |
| 66 | pthread_cond_signal(&b->notfull); |
| 67 | pthread_mutex_unlock(&b->lock); |
| 68 | return data; |
| 69 | } |
| 70 | |
| 71 | /* A test program: one thread inserts integers from 1 to 10000, |
| 72 | the other reads them and prints them. */ |
| 73 | |
| 74 | #define OVER (-1) |
| 75 | |
| 76 | struct prodcons buffer; |
| 77 | |
| 78 | static void * producer(void * data) |
| 79 | { |
| 80 | int n; |
| 81 | for (n = 0; n < 10000; n++) { |
| 82 | printf("%d --->\n", n); |
| 83 | put(&buffer, n); |
| 84 | } |
| 85 | put(&buffer, OVER); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | static void * consumer(void * data) |
| 90 | { |
| 91 | int d; |
| 92 | while (1) { |
| 93 | d = get(&buffer); |
| 94 | if (d == OVER) break; |
| 95 | printf("---> %d\n", d); |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | int main(void) |
| 101 | { |
| 102 | pthread_t th_a, th_b; |
| 103 | void * retval; |
| 104 | |
| 105 | init(&buffer); |
| 106 | /* Create the threads */ |
| 107 | pthread_create(&th_a, NULL, producer, 0); |
| 108 | pthread_create(&th_b, NULL, consumer, 0); |
| 109 | /* Wait until producer and consumer finish. */ |
| 110 | pthread_join(th_a, &retval); |
| 111 | pthread_join(th_b, &retval); |
| 112 | return 0; |
| 113 | } |