This commit is contained in:
Nikolaj
2021-10-27 13:50:32 +02:00
parent 456adc7611
commit 282a9a0f77
5 changed files with 21 additions and 18 deletions

View File

@ -1,6 +1,5 @@
CC=gcc
#CFLAGS=-g -Wall -Wextra -pedantic -std=gnu99 -pthread
CFLAGS=-g -pthread
CFLAGS=-g -Wall -Wextra -pedantic -std=gnu99 -pthread
EXAMPLES=fibs fauxgrep fauxgrep-mt fhistogram fhistogram-mt
.PHONY: all test clean ../src.zip

View File

@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "job_queue.h"
@ -17,7 +18,7 @@ int job_queue_init(struct job_queue *job_queue, int capacity) {
job_queue->capacity = capacity;
job_queue->size = 0;
job_queue->jobs = malloc(capacity);
job_queue->jobs = malloc(sizeof(void*) * capacity);
pthread_mutex_unlock(&queue_operation);
pthread_mutex_unlock(&queue_push);
@ -26,15 +27,10 @@ int job_queue_init(struct job_queue *job_queue, int capacity) {
}
int job_queue_destroy(struct job_queue *job_queue) {
printf("esauc\n");
pthread_mutex_lock(&queue_destroy);
pthread_mutex_lock(&queue_operation);
printf("esauc\n");
//free(job_queue->jobs);
printf("esauc\n");
//free(job_queue);
printf("esauc\n");
free(job_queue->jobs);
pthread_mutex_unlock(&queue_push);
pthread_mutex_unlock(&queue_pop);
@ -47,7 +43,7 @@ int job_queue_push(struct job_queue *job_queue, void *data) {
pthread_mutex_lock(&queue_push);
pthread_mutex_lock(&queue_operation);
(&job_queue->jobs)[job_queue->size] = data;
job_queue->jobs[job_queue->size] = data;
job_queue->size = job_queue->size + 1;
if (job_queue->size != job_queue->capacity) {
@ -74,7 +70,7 @@ int job_queue_pop(struct job_queue *job_queue, void **data) {
}
job_queue->size = job_queue->size - 1;
*data = (&job_queue->jobs)[job_queue->size];
*data = job_queue->jobs[job_queue->size];
if (job_queue->size == 0) {
pthread_mutex_unlock(&queue_destroy);

View File

@ -6,7 +6,7 @@
struct job_queue {
int capacity;
int size;
void* jobs;
void** jobs;
};
// Initialise a job queue with the given capacity. The queue starts out

Binary file not shown.

View File

@ -5,12 +5,20 @@
int main() {
struct job_queue q;
int b = 5;
int a;
int c;
void* b = malloc(1);
job_queue_init(&q, 64);
job_queue_push(&q, &b);
void* a = malloc(1);
job_queue_pop(&q, &a);
printf("%i\n",*((int *) a));
printf("esauc\n");
a = 1;
job_queue_push(&q, &a);
c = 2;
job_queue_push(&q, &c);
job_queue_pop(&q, &b);
printf("%i\n",*((int *) b));
job_queue_pop(&q, &b);
printf("%i\n",*((int *) b));
job_queue_destroy(&q);
}