Reputation: 193
Building a program for a lab I have to do that uses threads, I'm kind of lost with it, but I'm close to getting it compiled. I have 2 errors: one mentioned in the title, and the other is the same thing but it says invalid conversion from 'void*' to 'int'.
The errors occur on lines 98 and 124 within the producer and consumer threads, and I've marked them in the code. It's quite a bit of code, but I didn't know how to really shrink it down for this, sorry.
// 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);
}
I've done some reading on other threads similar to this, but I can't find the answer I'm looking for. Any help is appreciated, and I'm very new to this type of programming.
Upvotes: 2
Views: 10374
Reputation: 858
You declare functions as
void * producer(void *); // function for producer thread
void * consumer(void *); // function for consumer thread
But their definitions have another prototypes:
void * producer(void * buf[]);
void * consumer(void * buf[]);
In this case buf
is array of pointers to void
. And you are trying to assing number to pointer to void
:
buf[nextin++] = num;
Function prototypes must be the same in declaration and definition.
It is clear that you want to write numbers to the buffer. But you cannot have array of void
s. So cast buf
to int *
:
static_cast<int*>(buf)[nextin++] = num;
Upvotes: 4