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 CC=gcc
#CFLAGS=-g -Wall -Wextra -pedantic -std=gnu99 -pthread CFLAGS=-g -Wall -Wextra -pedantic -std=gnu99 -pthread
CFLAGS=-g -pthread
EXAMPLES=fibs fauxgrep fauxgrep-mt fhistogram fhistogram-mt EXAMPLES=fibs fauxgrep fauxgrep-mt fhistogram fhistogram-mt
.PHONY: all test clean ../src.zip .PHONY: all test clean ../src.zip

View File

@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <string.h>
#include "job_queue.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->capacity = capacity;
job_queue->size = 0; 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_operation);
pthread_mutex_unlock(&queue_push); 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) { int job_queue_destroy(struct job_queue *job_queue) {
printf("esauc\n");
pthread_mutex_lock(&queue_destroy); pthread_mutex_lock(&queue_destroy);
pthread_mutex_lock(&queue_operation); pthread_mutex_lock(&queue_operation);
printf("esauc\n"); free(job_queue->jobs);
//free(job_queue->jobs);
printf("esauc\n");
//free(job_queue);
printf("esauc\n");
pthread_mutex_unlock(&queue_push); pthread_mutex_unlock(&queue_push);
pthread_mutex_unlock(&queue_pop); 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_push);
pthread_mutex_lock(&queue_operation); 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; job_queue->size = job_queue->size + 1;
if (job_queue->size != job_queue->capacity) { 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; 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) { if (job_queue->size == 0) {
pthread_mutex_unlock(&queue_destroy); pthread_mutex_unlock(&queue_destroy);

View File

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

Binary file not shown.

View File

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