I am trying to create a stream and, as far as I remember, this should be the correct way:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
int SharedVariable =0;
void SimpleThread(int which)
{
int num,val;
for(num=0; num<20; num++){
if(random() > RAND_MAX / 2)
usleep(10);
val = SharedVariable;
printf("*** thread %d sees value %d\n", which, val);
SharedVariable = val+1;
}
val=SharedVariable;
printf("Thread %d sees final value %d\n", which, val);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t< NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, SimpleThread, (void* )t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
And the error I get is the following:
test.c: In the function 'main: test.c: 28: warning: passing argument 3 of' Pthread_create from an incompatible pointer type /usr/include/pthread.h:227: note: expected 'void * (*) (void * ), but the argument is of type 'void (*) (int)
I cannot change the SimpleThread function, so changing the type of the parameter is not a parameter, although I already tried it and it did not work either.
What am I doing wrong?
source
share