| 58 | /* Read and remove an integer from the buffer */ |
| 59 | |
| 60 | static int get(struct prodcons * b) |
| 61 | { |
| 62 | int data; |
| 63 | pthread_mutex_lock(&b->lock); |
| 64 | /* Wait until buffer is not empty */ |
| 65 | while (b->writepos == b->readpos) { |
| 66 | pthread_cond_wait(&b->notempty, &b->lock); |
| 67 | } |
| 68 | /* Read the data and advance read pointer */ |
| 69 | data = b->buffer[b->readpos]; |
| 70 | b->readpos++; |
| 71 | if (b->readpos >= BUFFER_SIZE) b->readpos = 0; |
| 72 | /* Signal that the buffer is now not full */ |
| 73 | pthread_cond_signal(&b->notfull); |
| 74 | pthread_mutex_unlock(&b->lock); |
| 75 | return data; |
| 76 | } |
| 77 | |
| 78 | /* A test program: one thread inserts integers from 1 to 10000, |
| 79 | the other reads them and prints them. */ |
no test coverage detected