I've made a queue header file and I've tried to use it with threads. What I'm doing is making 2 threads, 1 for reading the characters from the code file and entering the characters to the queue and the other thread is trying to print the characters to the console. The problem is that no characters are being printed to the console and I can't figure out why.
queue.h :
#ifndef QUEUE_INT
#define QUEUE_INT
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct
{
int *elementData;
unsigned int queueSize;
unsigned int capacityIncrement;
unsigned int elementCount;
} Queue;
void queue_initialize(Queue*, unsigned int);
int queue_add(Queue*, int);
void queue_poll(Queue*);
int queue_peek(const Queue*);
void queue_destroy(Queue*);
bool queue_isEmpty(const Queue*);
void queue_setCapacityIncrement(Queue*, unsigned int);
unsigned int queue_getCapacityIncrement(const Queue*);
unsigned int queue_getNumberOfElements(const Queue*);
unsigned int queue_getSize(const Queue*);
void queue_initialize(Queue *p, unsigned int capacityIncrement)
{
p->elementData = NULL;
p->queueSize = 0;
p->capacityIncrement = capacityIncrement;
p->elementCount = 0;
}
int queue_add(Queue *p, int value)
{
if(p->elementCount == p->queueSize)
{
int newQueueSize = p->queueSize + p->capacityIncrement;
void *temp = realloc(p->elementData, sizeof(*p->elementData) * newQueueSize);
if(temp == NULL || newQueueSize == 0)
{
return 1;
}
p->queueSize = newQueueSize;
p->elementData = temp;
}
p->elementData[p->elementCount] = value;
p->elementCount++;
return 0;
}
void queue_poll(Queue *p)
{
if(!queue_isEmpty(p))
{
p->elementCount--;
if(p->queueSize - p->elementCount == p->capacityIncrement / 2 + p->capacityIncrement)
{
int newQueueSize = p->queueSize - p->capacityIncrement;
p->elementData = realloc(p->elementData, sizeof(*p->elementData) * newQueueSize);
p->queueSize = newQueueSize;
}
for(int i = 0; i < p->elementCount; i++)
{
p->elementData[i] = p->elementData[i + 1];
}
}
}
int queue_peek(const Queue *p)
{
if(!queue_isEmpty(p))
{
return p->elementData[0];
}
return 0;
}
void queue_destroy(Queue *p)
{
free(p);
}
bool queue_isEmpty(const Queue *p)
{
return p->elementCount == 0;
}
void queue_setCapacityIncrement(Queue *p, unsigned int capacityIncrement)
{
p->capacityIncrement = capacityIncrement;
}
unsigned int queue_getCapacityIncrement(const Queue *p)
{
return p->capacityIncrement;
}
unsigned int queue_getNumberOfElements(const Queue *p)
{
return p->elementCount;
}
unsigned int queue_getSize(const Queue *p)
{
return p->queueSize;
}
#endif
Code file :
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <time.h>
#include "queue.h"
bool isFillQueueThreadRunning;
bool isQueueProcessing;
void fillQueueThread(void*);
void popQueueThread(void*);
int main()
{
srand(time(NULL));
Queue q1;
queue_initialize(&q1, 4);
HANDLE hFillQueueThread = (HANDLE)_beginthread(fillQueueThread, 0, (void*)&q1);
HANDLE hPopQueueThread = (HANDLE)_beginthread(fillQueueThread, 0, (void*)&q1);
WaitForSingleObject(hFillQueueThread, 1000 * 300);
WaitForSingleObject(hPopQueueThread, 1000 * 300);
return 0;
}
void fillQueueThread(void *p)
{
isFillQueueThreadRunning = true;
Queue *q = (Queue*)p;
FILE *f = fopen(__FILE__, "r");
int b;
while((b = getc(f)) != EOF)
{
Sleep(rand() % 50);
while(isQueueProcessing)
{
}
isQueueProcessing = true;
if (queue_add(q, b) == 1)
{
break;
}
isQueueProcessing = false;
}
fclose(f);
isFillQueueThreadRunning = false;
}
void popQueueThread(void *p)
{
Queue *q = (Queue*)p;
Sleep(10);
int b;
while(isFillQueueThreadRunning || q->elementCount > 0)
{
while(isQueueProcessing)
{
}
isQueueProcessing = true;
b = queue_peek(q);
queue_poll(q);
putchar(b);
isQueueProcessing = false;
}
}