gpt4 book ai didi

c - 使用结构体和多线程时出现 malloc.c 错误

转载 作者:行者123 更新时间:2023-11-30 14:23:06 26 4
gpt4 key购买 nike

我收到一个错误,但我不知道如何修复。我正在尝试使用自旋锁解决“生产者-消费者”问题。我创建了一个类似“队列”的数据结构作为共享资源,用于放入“生产的”项目,并删除要“消耗”的项目。这是我的主程序的样子:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "Queue.h"

#define DEBUG 1

Queue_t* global_queue; // create a global queue

/* thread procedure for the producer thread */
void* producer_func(void* arg)
{
while(1) // loops infinitely
{
int datum = rand() % global_queue->maximum_count;

// spin while the queue is full
while ((global_queue->current_count) ==
(global_queue->maximum_count));

enqueue(global_queue, datum);
display(global_queue);
}
}


/* thread procedure for the consumer thread */
void* consumer_func(void* arg)
{
while(1) // loops infinitely
{
int datum = 0;

// spin while there are no items in the queue
while(global_queue->current_count == 0);

datum = dequeue(global_queue);
printf("The number consumed is %d\n");
}
}


/* Main */
int main(int argc, char** argv)
{
if(argc != 2)
{
printf("Error: wrong number of command-line arguments\n");
printf("Usage: %s <integer>\n", argv[0]);
exit(1);
}

pthread_t producer; // create producer thread
pthread_t consumer; // create consumer thread

// create the queue object, get the max queue size
//int max_count = atoi(argv[1]);
global_queue = construct(10);
display(global_queue);

// intialize the random seed generator
srand((unsigned)time(NULL));

// create the threads and have them execute their routines
pthread_create(&producer, NULL, &producer_func, NULL);
pthread_create(&consumer, NULL, &consumer_func, NULL);

// join the threads to finish
pthread_join(producer, NULL);
pthread_join(consumer, NULL);

// deallocate the queue from memory
//destruct(global_queue);

return 0;
}

但是,当我在主例程中调用 pthread_create 时,我在 malloc.c 中收到一个奇怪的错误:

queue_demo: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

这是我的“队列”数据结构头文件:

#ifndef QUEUE_H
#define QUEUE_H

typedef struct Queue
{
int current_count;
int maximum_count;
int buffer[]; // queue uses an array
} Queue_t;


// routines to implement Queue-like functionality (FIFO)
// TODO: somehow encapsulate all these features in the struct itself.
//
Queue_t* construct(int buff_size);
void destruct (Queue_t* queue);
void display (Queue_t* queue);
int dequeue (Queue_t* queue);
void enqueue (Queue_t* queue, const int datum);

#endif

和实现:

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "Queue.h"

Queue_t* construct(int buff_size)
{
Queue_t* queue = malloc(sizeof(Queue_t));

assert(queue != NULL);
queue->maximum_count = buff_size;
queue->current_count = 0;

int i = 0;

for(; i < queue->maximum_count; ++i)
queue->buffer[i] = 0;

return queue;
}

void destruct(Queue_t* queue)
{
assert(queue != NULL);
free(queue);
printf("Queue destroyed!\n");
}

void display(Queue_t* queue)
{
int i = 0;

for(; i < queue->maximum_count; ++i)
printf("%d ", queue->buffer[i]);
printf("\n");
}

void enqueue(Queue_t* queue, const int datum)
{
assert(queue->current_count < queue->maximum_count);
queue->buffer[queue->current_count] = datum;
++queue->current_count;
}


int dequeue(Queue_t* queue)
{
int i = 1;
int datum = queue->buffer[0];

assert(queue->current_count > 0);

for(; i < queue->maximum_count; ++i)
{
queue->buffer[i-1] = queue->buffer[i];
queue->buffer[i] = 0;
}

--queue->current_count;

return datum;
}

显然,我做了一些非常错误的事情。但我不知道那到底是什么。我怀疑这可能与全局声明结构有关,但我不确定。任何想法将不胜感激。

最佳答案

您的construct()没有为队列项数据分配空间。

Queue_t* construct(int buff_size)
{
Queue_t* queue = malloc(sizeof(Queue_t));

assert(queue != NULL);
queue->maximum_count = buff_size;
queue->current_count = 0;

int i = 0;

// HERE. Where is the item data space allocation ??
for(; i < queue->maximum_count; ++i)
queue->buffer[i] = 0;

return queue;
}

试试这个:

Queue_t* construct(int buff_size)
{
// note space allocation for buffer[] bytes as well as overall structure.
Queue_t* queue = malloc(sizeof(Queue_t) + sizeof(int) * buff_size);

assert(queue != NULL);
queue->maximum_count = buff_size;
queue->current_count = 0;
memset(queue->buffer, 0, sizeof(int)*buff_size);
return queue;
}

我还没有审查您的其余代码来确定您是否正确保护了对队列的并发访问(pthread 的提及让我怀疑您应该向 Queue_t 类型的成员添加并初始化 pthread_mutex_t ,并用它来保护并发队列修改)。无论如何,上述问题绝对是一个问题,应该首先解决。

关于c - 使用结构体和多线程时出现 malloc.c 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13206340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com