-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueThreading.c
More file actions
129 lines (108 loc) · 2.95 KB
/
queueThreading.c
File metadata and controls
129 lines (108 loc) · 2.95 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
volatile int global_number = 0;
pthread_mutex_t muxlock = PTHREAD_MUTEX_INITIALIZER;
int threadAmount = 2;
struct Queue *q;
int running = 1;
struct QNode {
int key;
struct QNode *next;
};
struct Queue {
struct QNode *front, *rear;
};
struct QNode* newNode(int k) {
struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue *createQueue() {
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue *q, int k) {
// Create a new LL node
struct QNode *temp = newNode(k);
// If queue is empty, then new node is front and rear both
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}
struct QNode *deQueue(struct Queue *q) {
// If queue is empty, return NULL.
if (q->front == NULL)
return NULL;
// Store previous front and move front one node ahead
struct QNode *temp = q->front;
q->front = q->front->next;
// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->rear = NULL;
return temp;
}
struct threadData {
unsigned int threadID;
};
void *thread_code_dequeuer(void *arg) {
struct threadData * data = (struct threadData *) arg;
struct QNode *value;
while (running) {
pthread_mutex_lock(&muxlock);
value = deQueue(q);
pthread_mutex_unlock(&muxlock);
if (value != NULL) {
printf("I am thread %d and I have dequeued %d from the queue.\n", data->threadID, value->key);
} else {
printf("I am thread %d and the queue appears empty.\n", data->threadID);
sleep(1);
}
}
}
int main(void) {
q = createQueue();
pthread_t threads[threadAmount];
int i;
for (i = 0; i < threadAmount; i++) {
struct threadData *data = malloc(sizeof(struct threadData));
data->threadID = i;
pthread_create(&threads[i], NULL, &thread_code_dequeuer, (void *) data);
}
sleep(3);
for (i = 0; i < 10; i++) {
pthread_mutex_lock(&muxlock);
enQueue(q, i);
pthread_mutex_unlock(&muxlock);
}
/*
pthread_t threads[threadAmount];
int i;
for (i = 0; i < threadAmount; i++) {
struct threadData *data = malloc(sizeof(struct threadData));
data->threadID = i;
pthread_create(&threads[i], NULL, &thread_code, (void *) data);
}*/
/*
for (i = 0; i < threadAmount+1; ++i) {
void *rv;
pthread_join(threads[i], &rv);
}*/
sleep(3);
//running = 0;
printf("All threads finished executing\n");
}