繁体   English   中英

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

[英]error: invalid conversion from 'int' to 'void*' [-fpermissive]

为实验室构建一个程序,我必须使用线程,我对此有点迷惑,但是我接近将其编译。 我有2个错误:标题中提到了一个错误,另一个错误是相同的,但是它表示从'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);
}

我已经在与此类似的其他线程上进行了一些阅读,但找不到所需的答案。 感谢您的帮助,对于这种类型的编程我还是很陌生。

您将函数声明为

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的数组。 因此将bufint *

static_cast<int*>(buf)[nextin++] = num;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM