2 * double-buffering - an example of double-buffering with pthreads
4 * Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * NOTE: the synchronization scheme used in this program assume that we want
22 * to consume ALL the buffer produced, exactly ONE time.
24 * In other cases when buffer skipping or buffer duplication in wanted the
25 * synchronization scheme can change.
31 #include <sys/types.h>
36 #define BUFFER_SIZE 10
40 * The time spent by the producer and the consumer thread is quite predictable
41 * in this program, this may not be the case when some communication with
42 * actual hardware influences the production or consumption phase.
44 #define PRODUCER_DELAY 50000
45 #define CONSUMER_DELAY 20000
47 #define NUM_ITERATIONS 23
54 struct double_buffer_context {
55 struct buffer buffer[2];
56 int back_buffer_index;
57 int font_buffer_is_consumable;
58 pthread_cond_t front_buffer_cond;
59 pthread_mutex_t mutex;
62 #define BACK_BUFFER(ctx) ((ctx)->buffer[(ctx)->back_buffer_index])
63 #define FRONT_BUFFER(ctx) ((ctx)->buffer[(ctx)->back_buffer_index ^ 1])
64 #define SWAP_BUFFERS(ctx) ((ctx)->back_buffer_index ^= 1)
66 static void do_log(char *message)
71 clock_gettime(CLOCK_MONOTONIC, &now);
72 timestamp = now.tv_sec + now.tv_nsec / 1000000000.0;
73 fprintf(stderr, "[%f] %s\n", timestamp, message);
76 /* the consumer thread will only read from the front buffer */
77 static void *consumer_cb(void *arg)
79 struct double_buffer_context *ctx = (struct double_buffer_context *) arg;
86 pthread_mutex_lock(&ctx->mutex);
87 while (!ctx->font_buffer_is_consumable) {
88 pthread_cond_wait(&ctx->front_buffer_cond, &ctx->mutex);
90 pthread_mutex_unlock(&ctx->mutex);
92 /* consume front buffer */
93 len = snprintf(message, MAXLEN, "Consuming value: ");
94 for (i = 0; i < FRONT_BUFFER(ctx).size; i++) {
95 len += snprintf(message + len, MAXLEN - len, "%d ", FRONT_BUFFER(ctx).data[i]);
97 usleep(CONSUMER_DELAY);
100 pthread_mutex_lock(&ctx->mutex);
101 ctx->font_buffer_is_consumable = 0;
102 pthread_cond_signal(&ctx->front_buffer_cond);
103 pthread_mutex_unlock(&ctx->mutex);
113 pthread_t consumer_tid;
114 struct double_buffer_context *ctx;
116 unsigned int iterations;
119 char message[MAXLEN];
122 ctx = malloc(sizeof(*ctx));
127 memset(ctx, 0, sizeof(*ctx));
129 ctx->buffer[0].data = malloc(BUFFER_SIZE * sizeof(*ctx->buffer[0].data));
130 ctx->buffer[0].size = BUFFER_SIZE;
131 ctx->buffer[1].data = malloc(BUFFER_SIZE * sizeof(*ctx->buffer[1].data));
132 ctx->buffer[1].size = BUFFER_SIZE;
134 ctx->back_buffer_index = 0;
135 ctx->font_buffer_is_consumable = 0;
136 pthread_mutex_init(&ctx->mutex, NULL);
137 pthread_cond_init(&ctx->front_buffer_cond, NULL);
139 ret = pthread_create(&consumer_tid, NULL, consumer_cb, (void *)ctx);
142 fprintf(stderr, "can't create thread: %s\n", strerror(ret));
146 /* fixed seed, always the same sequence */
148 iterations = NUM_ITERATIONS;
149 while (iterations--) {
150 /* produce: in the back buffer */
151 len = snprintf(message, MAXLEN, "Producing value: ");
152 for (i = 0; i < BACK_BUFFER(ctx).size; i++) {
153 BACK_BUFFER(ctx).data[i] = rand();
154 len += snprintf(message + len, MAXLEN - len, "%d ", BACK_BUFFER(ctx).data[i]);
156 usleep(PRODUCER_DELAY);
159 pthread_mutex_lock(&ctx->mutex);
160 while (ctx->font_buffer_is_consumable) {
161 pthread_cond_wait(&ctx->front_buffer_cond, &ctx->mutex);
166 /* signal that the front buffer is ready to be consumed */
167 ctx->font_buffer_is_consumable = 1;
168 pthread_cond_signal(&ctx->front_buffer_cond);
170 pthread_mutex_unlock(&ctx->mutex);
173 /* wait for the last consumer run before terminating the thread */
174 pthread_mutex_lock(&ctx->mutex);
175 while (ctx->font_buffer_is_consumable) {
176 pthread_cond_wait(&ctx->front_buffer_cond, &ctx->mutex);
178 pthread_mutex_unlock(&ctx->mutex);
180 pthread_cancel(consumer_tid);
181 pthread_join(consumer_tid, NULL);
183 free(ctx->buffer[1].data);
184 free(ctx->buffer[0].data);