(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / Examples / ex2.c
blobf2556a4206a1a2e1a4dd10d8f298c1853a55fbfe
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. */
6 #include <stdio.h>
7 #include "pthread.h"
9 #define BUFFER_SIZE 16
11 /* Circular buffer of integers. */
13 struct prodcons
15 int buffer[BUFFER_SIZE]; /* the actual data */
16 pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
17 int readpos, writepos; /* positions for reading and writing */
18 pthread_cond_t notempty; /* signaled when buffer is not empty */
19 pthread_cond_t notfull; /* signaled when buffer is not full */
22 /* Initialize a buffer */
23 static void
24 init (struct prodcons *b)
26 pthread_mutex_init (&b->lock, NULL);
27 pthread_cond_init (&b->notempty, NULL);
28 pthread_cond_init (&b->notfull, NULL);
29 b->readpos = 0;
30 b->writepos = 0;
33 /* Store an integer in the buffer */
34 static void
35 put (struct prodcons *b, int data)
37 pthread_mutex_lock (&b->lock);
38 /* Wait until buffer is not full */
39 while ((b->writepos + 1) % BUFFER_SIZE == b->readpos)
41 pthread_cond_wait (&b->notfull, &b->lock);
42 /* pthread_cond_wait reacquired b->lock before returning */
44 /* Write the data and advance write pointer */
45 b->buffer[b->writepos] = data;
46 b->writepos++;
47 if (b->writepos >= BUFFER_SIZE)
48 b->writepos = 0;
49 /* Signal that the buffer is now not empty */
50 pthread_cond_signal (&b->notempty);
51 pthread_mutex_unlock (&b->lock);
54 /* Read and remove an integer from the buffer */
55 static int
56 get (struct prodcons *b)
58 int data;
59 pthread_mutex_lock (&b->lock);
60 /* Wait until buffer is not empty */
61 while (b->writepos == b->readpos)
63 pthread_cond_wait (&b->notempty, &b->lock);
65 /* Read the data and advance read pointer */
66 data = b->buffer[b->readpos];
67 b->readpos++;
68 if (b->readpos >= BUFFER_SIZE)
69 b->readpos = 0;
70 /* Signal that the buffer is now not full */
71 pthread_cond_signal (&b->notfull);
72 pthread_mutex_unlock (&b->lock);
73 return data;
76 /* A test program: one thread inserts integers from 1 to 10000,
77 the other reads them and prints them. */
79 #define OVER (-1)
81 struct prodcons buffer;
83 static void *
84 producer (void *data)
86 int n;
87 for (n = 0; n < 10000; n++)
89 printf ("%d --->\n", n);
90 put (&buffer, n);
92 put (&buffer, OVER);
93 return NULL;
96 static void *
97 consumer (void *data)
99 int d;
100 while (1)
102 d = get (&buffer);
103 if (d == OVER)
104 break;
105 printf ("---> %d\n", d);
107 return NULL;
111 main (void)
113 pthread_t th_a, th_b;
114 void *retval;
116 init (&buffer);
117 /* Create the threads */
118 pthread_create (&th_a, NULL, producer, 0);
119 pthread_create (&th_b, NULL, consumer, 0);
120 /* Wait until producer and consumer finish. */
121 pthread_join (th_a, &retval);
122 pthread_join (th_b, &retval);
123 return 0;