-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcbuffer_demo.c
79 lines (61 loc) · 1.78 KB
/
pcbuffer_demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/****************************************************************************
FILE : pcbuffer_demo.c
LAST REVISION : 2008-09-15
SUBJECT : Test program to exercise pcbuffer objects.
PROGRAMMER : (C) Copyright 2008 by Peter Chapin
This program is very minimal; a large number of interesting cases are not
exercised (or at least not necessarily exercised). Using this program will
at least verify that the functions in pcbuffer.c compile and do something
useful without, for example, dumping core immediately. Building a more
complete test program would be nice. Maybe someday.
Please send comments or bug reports to
Peter Chapin
Computer Information Systems
Vermont Technical College
Randolph Center, VT 05061
Peter.Chapin@vtc.vsc.edu
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "pcbuffer.h"
#define OBJECT_COUNT 10000
pcbuffer my_buffer;
void *producer(void *arg)
{
int i;
int *p;
for (i = 1; i <= OBJECT_COUNT; i++) {
p = malloc(sizeof(int));
*p = i;
pcbuffer_push(&my_buffer, p);
if (i % 1000 == 0) printf("P: %5d\n", i);
}
return NULL;
}
void *consumer(void *arg)
{
int i;
int *p;
for (i = 1; i <= OBJECT_COUNT; i++) {
p = pcbuffer_pop(&my_buffer);
if (*p != i) {
printf("C: Unexpected item from producer: %d\n", *p);
}
free(p);
if (i % 1000 == 0) printf("C: %5d\n", i);
}
return NULL;
}
int main(void)
{
pthread_t p_ID, c_ID;
pcbuffer_init(&my_buffer);
pthread_create(&p_ID, NULL, producer, NULL);
pthread_create(&c_ID, NULL, consumer, NULL);
// Let the threads do their thing.
pthread_join(c_ID, NULL);
pthread_join(p_ID, NULL);
pcbuffer_destroy(&my_buffer);
return 0;
}