之前的声明是 - 这里和“put_non_bloccante的冲突类型”

时间:2013-11-29 15:04:19

标签: c function declaration

对不起,我的文件buffer.c中有一个错误,上面写着“冲突类型” 在我的文件buffer.h中,它说“put_non_bloccante的先前声明就在这里!!

/*
 * Buffer.c
 *
 *  Created on: 19/nov/2013
 *      Author: lele
 */
#include "buffer.h"
#include <stdio.h>
#include "msg.h"
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_ERROR (msg_t *) NULL

/* allocazione / deallocazione buffer */
// creazione di un buffer vuoto di dim. max nota





    buffer_t * buffer_init(unsigned int maxsize){
        buffer_t * new_buffer = (buffer_t*)malloc( sizeof(buffer_t) );
        new_buffer->T=0;
        new_buffer->D=0;
        new_buffer->msg_presenti=0;
        new_buffer->array_msg =(msg_t **)malloc(sizeof(msg_t*) * maxsize);
        new_buffer->size=maxsize;
        pthread_cond_init(&(new_buffer->not_empty),NULL);
        pthread_cond_init(&(new_buffer->not_full),NULL);
        pthread_mutex_init(&(new_buffer->mutex),NULL);

        return new_buffer;
    }
    // deallocazione di un buffer



    void buffer_destroy(buffer_t* buffer){
        int i;
        for(i=0;i<buffer->size;i++){
            msg_destroy_string(buffer->array_msg[i]);

        }
        free(buffer->array_msg);
        free(buffer);




    }


    msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg){
        // inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
            // altrimenti effettua l'inserimento e restituisce il messaggio
            // inserito; N.B.: msg!=null
       if( (pthread_mutex_trylock(&(buffer->mutex))) !=0){

           return BUFFER_ERROR;

       }
        while(buffer->msg_presenti==buffer->size){
            pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));

    }
        buffer->array_msg[buffer->D]=msg;
        buffer->D = (buffer->D+1)%buffer->size;
        buffer->msg_presenti= buffer->msg_presenti+1;
        pthread_cond_signal(&(buffer->not_empty));
        pthread_mutex_unlock(&(buffer->mutex));
        return msg;
    }
    // estrazione bloccante: sospende se vuoto, quindi
    // restituisce il valore estratto non appena disponibile
    msg_t* get_bloccante(buffer_t* buffer){
    // estrazione non bloccante: restituisce BUFFER_ERROR se vuoto
    // ed il valore estratto in caso contrario

        pthread_mutex_lock(&(buffer->mutex));
        while(buffer->msg_presenti==0){
            pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
        }
        msg_t * msg =buffer->array_msg[buffer->T];
        buffer->T=(buffer->T+1)%buffer->size;
        buffer->msg_presenti--;
        pthread_cond_signal(&(buffer->not_full));
        pthread_mutex_unlock(&(buffer->mutex));
        return msg;

    }
    msg_t* get_non_bloccante(buffer_t* buffer){
        if( (pthread_mutex_trylock(&(buffer->mutex))) !=0){

                   return BUFFER_ERROR;

               }
                while(buffer->msg_presenti==0){
                    pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
                }
                msg_t* msg =buffer->array_msg[buffer->T];
                buffer->T=(buffer->T+1)%buffer->size;
                buffer->msg_presenti--;
                pthread_cond_signal(&(buffer->not_full));
                pthread_mutex_unlock(&(buffer->mutex));
                return msg;
    }


    msg_t* put_bloccante(buffer_t *buffer, msg_t *msg){
        // inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
            // altrimenti effettua l'inserimento e restituisce il messaggio
            // inserito; N.B.: msg!=null
       pthread_mutex_lock(&(buffer->mutex));
        while(buffer->msg_presenti==buffer->size){
            pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));

    }
        buffer->array_msg[buffer->D]=msg;
        buffer->D = (buffer->D+1)%buffer->size;
        buffer->msg_presenti= buffer->msg_presenti+1;
        pthread_cond_signal(&(buffer->not_empty));
        pthread_mutex_unlock(&(buffer->mutex));
        return msg;
    }

这是我的buffer.h文件!我该怎么办?:

#include <pthread.h>
#include "msg.h"
#define BUFFER_ERROR (msg_t *) NULL
typedef struct buffer {
    int size;
    int T;
    int D;
    int msg_presenti;

    pthread_cond_t not_full;
    pthread_cond_t not_empty;
    pthread_mutex_t mutex;
    msg_t ** array_msg;

} buffer_t;

    buffer_t * buffer_init(unsigned int maxsize);
    void buffer_destroy(buffer_t* buffer);
    msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg);

1 个答案:

答案 0 :(得分:1)

buffer.c包含“msg.h”,buffer.h也是如此。

解决此问题的最简单方法是将其放在msg.h的顶部

#ifndef MSG_H_
#define MSG_H_

这就在最底层

#endif

你应该用buffer.h做类似的事情。这避免了单个翻译单元中的多个包含。

相关问题