3

I am trying pthread example. Here is my code:

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

void*AsalK1(void *gelen);

int main(){

    int *i;
    i= new int;
    *i=1;

    int sonSayi;
    pthread_t th1, th2, th3, th4;
    printf("---------------------------------------------\n");
    printf("|       Threadler ile Asal Sayi Bulma       |\n");
    printf("---------------------------------------------\n");
    printf("Son sayi degeri: 1000000 \n");

    int r1=pthread_create( &th1, NULL, AsalK1, (void *)i);
    *i=3;
    int r2=pthread_create( &th2, NULL, AsalK1, (void *)i);
    *i=5;
    int r3=pthread_create( &th3, NULL, AsalK1, (void *)i);
    *i=7;
    int r4=pthread_create( &th4, NULL, AsalK1, (void *)i);

    pthread_join( th1, NULL);
    pthread_join( th2, NULL);
    pthread_join( th3, NULL);
    pthread_join( th4, NULL);
    return 0;
}

void *AsalK1(void *gelen){
    int bas= *gelen;
    printf("bas :&d\n",bas);    
}

and i use this code to compile :

gcc -lpthread ThreadDeneme.cpp

or

g++ -lpthread ThreadDeneme.cpp

And the Error Says:

cannot initialize a variable of type 'int' with an lvalue of type 'void' int bas= *gelen;

I use this:

int bas= (int *) gelen;

but error still in progress.

I read:

What is the difference between these uses of pointers?

CybeX
  • 2,060
  • 3
  • 48
  • 115
Nurullah
  • 103
  • 3
  • 4
  • 14

5 Answers5

6

1) You are not allowed to dereference a void *. So,

int bas= *gelen;

isn't going to work. The correct way to read the int is:

int bas= *(int*)gelen;

2) But this isn't going to work either. Because the same address i is passed to all the 4 threads and that results in data race. So, you need to pass different address to each thread in order to avoid data race (or use some form of synchronization).

3) Your thread function AsalK1() should return a pointer (See pthread_create()'s requirement for the thread function). You can simply return a NULL pointer since you are not returning anything from the thread to main thread.

4) Another potential problem is how you compile:

gcc -lpthread ThreadDeneme.cpp

The library should be at the end of the commandline option. So, you compile as:

gcc ThreadDeneme.cpp -lpthread

Another related suggestion: you can use array instead of using 4 different thread id variables (th1..4). It would allow to use a loop for thread creation and joining and you can easily change the number of threads as well.

P.P
  • 117,907
  • 20
  • 175
  • 238
1

Addressing the issue related to the error message, you need have a complete type before you can dereference that. Change

 int bas= *gelen;

to

int bas= *(int *)gelen;

FWIW, void is an incomplete type,

The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

For C++, quoting C++14, (emphasis mine)

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.” [ Note: indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue,]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

You'll have to use

int bas= *(int *) gelen;

First cast to pointer of type int then dereference it, using *

P0W
  • 46,614
  • 9
  • 72
  • 119
1
int bas= *gelen;

As you see in void *AsalK1(void *gelen) function gelen is void pointer and you must need to typecast to valid data type as in your case you are going to store in int bas since the bas is int data type so the gelen pointer should be typecase to int * as below

int bas= *(int *) gelen;
leuage
  • 566
  • 3
  • 17
0

You need to cast it to pointer of type int. You also have an error in printf.

void *AsalK1(void *gelen){
int *bas= (int *) gelen;
    printf("bas :%d\n",*bas);    
 }