微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不知道为什么会发生这个问题,也无法解决这个问题谁能解决这个问题?

如何解决不知道为什么会发生这个问题,也无法解决这个问题谁能解决这个问题?

这是生产消费者问题的示例代码。 在第 58,86,116,166,194 行中,在函数‘void* Producer1(void*)’中出错:从‘void*’到‘int’的转换失去了精度 [-fpermissive]。 仍然不知道为什么会发生此错误,也无法找出解决方案。

我使用 Ubuntu 作为操作系统和 g++ -pthread 库函数解决这个问题。 代码在这里

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include<semaphore.h>
//#include </usr/include/semaphore.h>

// for sleep
#include <unistd.h>

#define BUFF_SIZE   5           /* total number of slots */

typedef struct                //Producing queue
{
    char buf1[BUFF_SIZE];   /* shared var */
    int in1;               /* buf[in%BUFF_SIZE] is the first empty slot */
    int out1;              /* buf[out%BUFF_SIZE] is the first full slot */
    sem_t full;           /* keep track of the number of full spots */
    sem_t empty;          /* keep track of the number of empty spots */

    // use correct type here
    pthread_mutex_t mutex;          /* enforce mutual exclusion to shared data */
} sbuf_t1;

typedef struct               //Chokolate Cake queue
{
    char buf2[BUFF_SIZE];   /* shared var */
    int in2;               /* buf[in%BUFF_SIZE] is the first empty slot */
    int out2;              /* buf[out%BUFF_SIZE] is the first full slot */
    sem_t full;           /* keep track of the number of full spots */
    sem_t empty;          /* keep track of the number of empty spots */

    // use correct type here
    pthread_mutex_t mutex;          /* enforce mutual exclusion to shared data */
} sbuf_t2;

typedef struct               //Vanilla Cake queue
{
    char buf3[BUFF_SIZE];   /* shared var */
    int in3;               /* buf[in%BUFF_SIZE] is the first empty slot */
    int out3;              /* buf[out%BUFF_SIZE] is the first full slot */
    sem_t full;           /* keep track of the number of full spots */
    sem_t empty;          /* keep track of the number of empty spots */

    // use correct type here
    pthread_mutex_t mutex;          /* enforce mutual exclusion to shared data */
} sbuf_t3;

sbuf_t1 shared1;
sbuf_t2 shared2;
sbuf_t3 shared3;


void *Producer1(void *arg)
{
    int i,index;
    char item;

    index = (int)arg;
  
    for(i=0; i<10; ++i)
    {
        sem_wait(&shared1.empty);    //down called in this line
        pthread_mutex_lock(&shared1.mutex);     //acquired mutex lock Now it can produced no contex will be switched 
        sleep(1);  
        shared1.buf1[shared1.in1] = 'C'; //item pusch in buffer like item[in] = item;
        shared1.in1 = (shared1.in1+1)%BUFF_SIZE;  //queue operation q = (in + 1) % buff_size
        printf("[P%d] Producing Chokolate Cake...\n",index);
        fflush(stdout);
        /* Release the buffer */
        pthread_mutex_unlock(&shared1.mutex); // unclock the mutex 
        /* Increment the number of full slots */
        sem_post(&shared1.full);  //buffer is full 
        //if (i % 2 == 1) sleep(1);
    }


    return NULL;
}


void *Producer2(void *arg)
{
    int i,index;
    char item;

    index = (int)arg;
    

    for(i=0; i<10; ++i)
    {
        item = i;
        sem_wait(&shared1.empty);
        pthread_mutex_lock(&shared1.mutex);
        sleep(1);
        shared1.buf1[shared1.in1] = 'V';
        shared1.in1 = (shared1.in1+1)%BUFF_SIZE;
        printf("[P%d] Producing Vanilla Cake...\n",index);
        fflush(stdout);
        /* Release the buffer */
        pthread_mutex_unlock(&shared1.mutex);
        /* Increment the number of full slots */
        sem_post(&shared1.full);
        //if (i % 2 == 1) sleep(1);
    }


    return NULL;
}


void *Chef_Z(void *arg)
{
    int i,index;
    char item;

    index = (int)arg;

    for (i=10; i > 0; i--) {
        sem_wait(&shared1.full);
        pthread_mutex_lock(&shared1.mutex);
        sleep(1);
        item=shared1.buf1[shared1.out1];
        if(item == 'C')  // Chokolate Cake queue
        {
        sem_wait(&shared2.full);
            pthread_mutex_lock(&shared2.mutex);
            shared2.buf2[shared2.in2]=item; 
            shared2.in2 = (shared2.in2+1)%BUFF_SIZE;
        printf("[C_Z] Consuming  Chokolate Cake and stored it in Chokolate queue ...\n");
            pthread_mutex_unlock(&shared2.mutex);
            /* Increment the number of full slots */
            sem_post(&shared2.empty);

        }
        else if(item == 'V')  // Vanilla Cake queue
        {
        sem_wait(&shared3.full);
            pthread_mutex_lock(&shared3.mutex);
            shared3.buf3[shared3.in3]=item; 
            shared3.in3 = (shared3.in3+1)%BUFF_SIZE;
        printf("[C_Z] Consuming  Vanilla Cake and stored it in Vanilla queue ...\n");
            pthread_mutex_unlock(&shared3.mutex);
            /* Increment the number of full slots */
            sem_post(&shared3.empty);

        }
        shared1.out1 = (shared1.out1+1)%BUFF_SIZE;

        fflush(stdout);
        /* Release the buffer */
        pthread_mutex_unlock(&shared1.mutex);
        /* Increment the number of full slots */
        sem_post(&shared1.empty);

        /* Interleave  producer and consumer execution */
        //if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

void *Waiter1(void *arg) //Chokolate cake waiter
{
    int i,index;
    char item;

    index = (int)arg;
    
    for (i=10; i > 0; i--) {
        sem_wait(&shared2.full);
        pthread_mutex_lock(&shared2.mutex);
        sleep(1);

        item=shared2.buf2[shared2.out2];
        shared2.out2 = (shared2.out2+1)%BUFF_SIZE;
        printf("[W%d] Consuming  Chokolate Cake ...\n",index);
        fflush(stdout);
        /* Release the buffer */
        pthread_mutex_unlock(&shared2.mutex);
        /* Increment the number of full slots */
        sem_post(&shared2.empty);

        /* Interleave  producer and consumer execution */
        //if (i % 2 == 1) sleep(1);
    }
    return NULL;
}


void *Waiter2(void *arg)  // Vanilla cake waiter
{
    int i,index;
    char item;

    index = (int)arg;
    

    for (i=10; i > 0; i--) {
        sem_wait(&shared3.full);
        pthread_mutex_lock(&shared3.mutex);
        sleep(1);

        item=shared3.buf3[shared3.out3];
        shared3.out3 = (shared3.out3+1)%BUFF_SIZE;
        printf("[W%d] Consuming  Vanilla Cake ...\n",index);
        fflush(stdout);
        /* Release the buffer */
        pthread_mutex_unlock(&shared3.mutex);
        /* Increment the number of full slots */
        sem_post(&shared3.empty);

        /* Interleave  producer and consumer execution */
        //if (i % 2 == 1) sleep(1);
    }

    return NULL;
}


int main()
{
    //pthread_t idP,idC;
    pthread_t thread1,thread2,thread3,thread4,thread5;
    int index;

    void *producer1End;
    void *producer2End;
    void *chef_zEnd;
    void *waiter1End;
    void *waiter2End;

    sem_init(&shared1.full,0);
    sem_init(&shared1.empty,BUFF_SIZE);
    pthread_mutex_init(&shared1.mutex,NULL);

    sem_init(&shared2.full,0);
    sem_init(&shared2.empty,BUFF_SIZE);
    pthread_mutex_init(&shared2.mutex,NULL);

    sem_init(&shared3.full,0);
    sem_init(&shared3.empty,BUFF_SIZE);
    pthread_mutex_init(&shared3.mutex,NULL);

    pthread_create(&thread1,NULL,Producer1,(void*)1 );
    pthread_create(&thread2,Producer2,(void*)2 );
    pthread_create(&thread3,Chef_Z,(void*)1 );    

    pthread_create(&thread4,Waiter1,(void*)1);
    pthread_create(&thread5,Waiter2,(void*)2);

    pthread_join(thread1,&producer1End);
    pthread_join(thread2,&producer2End);
    pthread_join(thread3,&chef_zEnd);
    pthread_join(thread4,&waiter1End);
    pthread_join(thread5,&waiter2End);

    pthread_exit(NULL);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。