source: CIVL/examples/translation/pthread/queue_false.c@ be4355b

1.23 2.0 main test-branch
Last change on this file since be4355b was 5fb1a47, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added pthread examples

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@818 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100755
File size: 2.4 KB
Line 
1#include <pthread.h>
2#include <stdio.h>
3#include <assert.h>
4#include <stdlib.h>
5#define SIZE (20)
6#define EMPTY (-1)
7#define FULL (-2)
8#define FALSE (0)
9#define TRUE (1)
10
11typedef struct {
12 int element[SIZE];
13 int head;
14 int tail;
15 int amount;
16} QType;
17
18pthread_mutex_t m;
19int stored_elements[SIZE];
20_Bool enqueue_flag, dequeue_flag;
21QType queue;
22
23int init(QType *q)
24{
25 q->head=0;
26 q->tail=0;
27 q->amount=0;
28}
29
30int nondet()
31{
32 if (((double) rand() / (RAND_MAX+1.0)) > 0.5){
33 printf("1");
34 return 1;
35 }
36 printf("0");
37 return 0;
38}
39
40
41
42int empty(QType * q)
43{
44 if (q->head == q->tail)
45 {
46 printf("queue is empty\n");
47 return EMPTY;
48 }
49 else
50 return 0;
51}
52
53int full(QType * q)
54{
55 if (q->amount == SIZE)
56 {
57 printf("queue is full\n");
58 return FULL;
59 }
60 else
61 return 0;
62}
63
64int enqueue(QType *q, int x)
65{
66 q->element[q->tail] = x;
67 q->amount++;
68 if (q->tail == SIZE)
69 {
70 q->tail = 1;
71 }
72 else
73 {
74 q->tail++;
75 }
76
77 return 0;
78}
79
80int dequeue(QType *q)
81{
82 int x;
83
84 x = q->element[q->head];
85 q->amount--;
86 if (q->head == SIZE)
87 {
88 q->head = 1;
89 }
90 else
91 q->head++;
92
93 return x;
94}
95
96void *t1(void *arg)
97{
98 int value, i;
99
100 pthread_mutex_lock(&m);
101 printf("2");
102 value = nondet();
103 if (enqueue(&queue,value)) {
104 goto ERROR;
105 }
106
107 stored_elements[0]=value;
108 if (empty(&queue)) {
109 goto ERROR;
110 }
111
112 pthread_mutex_unlock(&m);
113
114 for(i=0; i<(SIZE-1); i++)
115 {
116 pthread_mutex_lock(&m);
117 printf("4");
118 if (enqueue_flag)
119 {
120 value = nondet();
121 enqueue(&queue,value);
122 stored_elements[i+1]=value;
123 enqueue_flag=FALSE;
124 dequeue_flag=TRUE;
125 }
126 pthread_mutex_unlock(&m);
127 }
128
129 return NULL;
130
131 ERROR:
132 goto ERROR;
133}
134
135void *t2(void *arg)
136{
137 int i;
138
139 for(i=0; i<SIZE; i++)
140 {
141 pthread_mutex_lock(&m);
142 printf("3");
143 if (dequeue_flag)
144 {
145 if (!dequeue(&queue)==stored_elements[i]) {
146 ERROR:
147 goto ERROR;
148 }
149 dequeue_flag=FALSE;
150 enqueue_flag=TRUE;
151 }
152 pthread_mutex_unlock(&m);
153 }
154
155 return NULL;
156}
157
158int main(void)
159{
160 pthread_t id1, id2;
161
162 enqueue_flag=TRUE;
163 dequeue_flag=FALSE;
164
165 init(&queue);
166
167 if (!empty(&queue)==EMPTY) {
168 ERROR:
169 goto ERROR;
170 }
171
172
173 pthread_mutex_init(&m, 0);
174
175 pthread_create(&id1, NULL, t1, &queue);
176 pthread_create(&id2, NULL, t2, &queue);
177
178 pthread_join(id1, NULL);
179 pthread_join(id2, NULL);
180
181 return 0;
182}
183
Note: See TracBrowser for help on using the repository browser.