错误:从'int'无效转换为'void *'[-fpermissive]

时间:2017-10-02 22:34:39

标签: c++

为实验室构建一个程序,我必须使用线程,我有点迷失它,但我接近于编译它。我有两个错误:一个在标题中提到,另一个是相同的,但它表示从'void *'到'int'的无效转换。

错误发生在生产者和消费者线程中的第98和124行,我在代码中标记了它们。这是相当多的代码,但我不知道如何真正缩减它,抱歉。

// syncA.cpp for lab2

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define BSIZE 10
#define NUM_ITEMS 10
#define NUM_THREADS 2


int buf[BSIZE];
int nextin=0, nextout=0;


void * producer(void *);    // function for producer thread
void * consumer(void *);    // function for consumer thread
pthread_mutex_t lock;

pthread_t tid[NUM_THREADS];      // array of thread IDs


int main( int argc, char *argv[] ) 
{
    int i;

    cout << "Creating threads" << endl;

    pthread_create(&tid[1], NULL, consumer, (void *) buf[BSIZE]);
    pthread_create(&tid[0], NULL, producer, (void *) buf[BSIZE]);

    for (i = 0; i < NUM_THREADS; i++){
        pthread_join(tid[i], NULL);
    }

    cout << "All threads have been terminated" << endl << endl;

    // Finding minimum
    int minimum = buf[1];

    for (i = 1; i <= BSIZE; i ++){
        if (minimum > buf[i + 1])
            minimum = buf[i + 1];
    }

    // Finding maximum
    int maximum = buf[1];

    for (i = 1; i <= BSIZE; i++){
        if (maximum < buf[i + 1])
            maximum = buf[i + 1];
    }

    // Finding average
    int average;
    int sum = 0;

    for (i = 1; i <= BSIZE; i++){
        sum = sum + buf[i];
    }

    average = sum / BSIZE;

    // Outputting claculated data
    cout << "Minimum value: " << minimum << endl;
    cout << "Maximum value: " << maximum << endl;
    cout << "Average value: " << average << endl;

    return 0;

}  /* main */



void * producer(void * buf[])
{
    int product; // For multiplying inside the for loop for the "wait"
    int num;

    cout << "Producer started" << endl;

    // Locking thread
    pthread_mutex_lock(&lock);

    // Producing 10 items and putting them in the buffer
    for (int i = 0; i < BSIZE; i++){
        num = rand() % 1000;

        // Using a for loop 1000 times to act as the wait
        for (int k = 0; k < 1000; k++){
            product = 8 * 9;
        }

        // Putting the num in the buffer at pos 1, 2, 3, etc
        buf[nextin++] = num; <---------------- ***ERROR***
    }
    // Unlocking thread
    pthread_mutex_unlock(&lock);

    // Exiting the producer
    pthread_exit(0);
}    

void * consumer(void * buf[])
{
    int num;
    int product;

    cout << "Consumer started" << endl << endl;

    // Locking thread
    pthread_mutex_lock(&lock);

    // Waiting before accessing buffer
    for (int k = 0; k < 1000; k++){
        product = 8 * 9;
    }

    //consuming items
    for (int i = 0; i < BSIZE; i++){
        num = buf[nextout++]; <---------------- ***ERROR***
        cout << "Consuming item: " << num << endl;
            // TODO: Consume item
    }

    // Unlocking thread
    pthread_mutex_unlock(&lock);

    // Exiting consumer
    pthread_exit(0);
}

我已经完成了与此类似的其他线程的阅读,但我找不到我正在寻找的答案。感谢任何帮助,我对这种类型的编程非常陌生。

1 个答案:

答案 0 :(得分:3)

您将函数声明为

void * producer(void *);    // function for producer thread
void * consumer(void *);    // function for consumer thread

但他们的定义有另一个原型:

void * producer(void * buf[]);
void * consumer(void * buf[]);

在这种情况下,buf是指向void的指针数组。而你正试图将数字指向void

buf[nextin++] = num;

函数原型在声明和定义中必须相同。

很明显,您想要将数字写入缓冲区。但你不能拥有void的数组。所以将buf投射到int *

static_cast<int*>(buf)[nextin++] = num;
相关问题