-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqueue_tests.c
74 lines (71 loc) · 1.56 KB
/
queue_tests.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
/**
* Luscious Locks Lab
* CS 241 - Fall 2016
*/
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "queue.h"
typedef struct queue_node_t {
struct queue_node_t *next;
void *data;
} queue_node_t;
struct queue_t {
queue_node_t *head, *tail;
int size;
int maxSize;
pthread_cond_t cv;
pthread_mutex_t m;
};
queue_t* queue;
void* pusher(void *p){
int i = (*(int*)p);
int j = 0;
for(j = 0; j < i ;j++){
int* i = malloc(sizeof(int));
*i = j;
queue_push(queue,(void*)(i));
}
printf("queue size is now %d\n",queue->size);
return NULL;
}
void* puller(void* p){
int i = (*(int*)p);
int n = (i/2) - 1;
int j = 0;
int sum = 0;
int predicted = 0;
for(j = 0; j < i ;j++){
//sum += *((int*)queue_pull(queue));
void * t = queue_pull(queue);
sum += *((int*)t);
free(t);
predicted += j;
}
printf("queue size is now %d\n",queue->size);
printf("The sum is %d and it should be %d\n",sum, (n*n + n) );
return NULL;
}
int main(int argc, char **argv) {
// pthread_t thread1, thread2,thread3;
// int i = 5000;
// int j = 2*i;
queue = queue_create(3);
queue_push(queue, (void *)1);
queue_push(queue, (void *)1);
printf("done pushing\n");
queue_pull(queue);
queue_pull(queue);
queue_pull(queue);
printf("did I make it\n");
// pthread_create(&thread1,NULL,pusher,(void*)(&i));
// pthread_create(&thread2,NULL,pusher,(void*)(&i));
// pthread_join(thread1,NULL);
// pthread_join(thread2,NULL);
// pthread_create(&thread3,NULL,puller,(void*)(&j));
// pthread_join(thread3,NULL);
queue_destroy(queue);
return 0;
}