3

I'm following this application by my book, and I tried to copy this program and see what it does.

With my big surprise I found that it doesn't work!!

The program is the following:

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

#define TANTI 10
int parametroOUT;

void* codice(void *arg){
    srand(time(NULL)); 
    parametroOUT=(rand()%6)+1;
    pthread_exit((void*)&parametroOUT);
}

int main(){
    int dadoEstratto, *risultato=0;
    pthread_t t1;
    pthread_create(&t1,NULL,codice,NULL);   
    pthread_join(t1, (void*) &risultato);
    printf("dado estratto: %d",*risultato);
    return 0;                                           
}

It returns an error at the line: pthread_join(t1, (void*) &risultato);.

The error is:

[Error] invalid conversion from 'void*' to 'void**' [-fpermissive]

How can I fix this error?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42

3 Answers3

1

This code is wrong:

int *risultato=0;
...
pthread_join(t1, (void*) &risultato);

The prototype for pthread_join() is:

int pthread_join(pthread_t thread, void **value_ptr);

Note that it's a void **. That's because the function used to start a thread returns a void *. To be able to access the void * returned from the thread-start function, you need to pass the address of a void * so that pthread_join() can fill in the value:

void *risultato=0;
...
pthread_join(t1, &risultato);

Note that risultato is now the proper void *, and it's address is passed to pthread_join() by using the & "address-of" operator.

To get back to the int return value from your codice() function:

int *intPtr = ( int * ) risultato;
int value = *intPtr;
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
0

The second argument should have type void**.

Here's what you should do:

void *risultato_voidp;
pthread_join(t1, &risultato_voidp);
resultato = static_cast<int*>(risultato_voidp);

Full example:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <time.h>
#include <errno.h>
#define TANTI 10
int parametroOUT;
void* codice(void *arg){
    srand(time(NULL));
    parametroOUT=(rand()%6)+1;
    pthread_exit(static_cast<void*>(&parametroOUT));
}
int main(){
    int dadoEstratto, *risultato=0;
    void *risultato_voidp;
    pthread_t t1;
    int er;
    if(0!=(er=pthread_create(&t1,NULL,codice,NULL))) return errno=er,perror(0),1;
    pthread_join(t1, &risultato_voidp);
    risultato = static_cast<int*>(risultato_voidp);
    printf("dado estratto: %d\n",*risultato);
    return 0;
}
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • It returns this error: [Error] cast from 'void*' to 'int' loses precision [-fpermissive] in the int resultato=(int) risultato_voidp; – Gabriele Santoro Mar 15 '19 at 22:30
  • it work but it give me a strange value the output should be a number between 1 and 6, but the outbut is this 0 4254373 – Gabriele Santoro Mar 15 '19 at 22:38
  • nothing, i copy the entirely example you made but the output is the same – Gabriele Santoro Mar 15 '19 at 22:43
  • @GabrieleSantoro It's "working" correctly. It's a weird example though. You can just print `parametroOUT` after the join, since it's a global, and forget about passing anything through `pthread_exit()`. – Petr Skocik Mar 15 '19 at 22:43
  • I've understood, I didin't think about put the print in the void. In a certain sense it was helpful, but i didn't undrestand why it made that number as output and not a number between 1 and 6. What is wrong? – Gabriele Santoro Mar 15 '19 at 22:46
0

I solved my problem by making this simple function, thank you to everyone who helped me

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <time.h>
#define TANTI 10
int dadi[TANTI];
int i=0; 
void* codice(void *arg){

 while(i<TANTI){
    srand(time(NULL);
  dadi[i]=rand() % 6 + 1;
  printf("il dado estratto e': %d\n",dadi[i]);
  i++;
      }
 pthread_exit(0);     
        }
int main(){
 int dadoEstratto;
 pthread_t t1;
 pthread_create(&t1,NULL,codice,NULL);
 pthread_join(t1,NULL);
 return 0;           
  }