Pthread_create does not work. passing argument 3

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);
      }
   }

   /* Last thing that main() should do */
   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?

+5
source share
2 answers

SimpleThread must be declared as

void* SimpleThread(void *args) {
}

, struct, struct void* .

+12

"" , , , . pthread_join .

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

#define NUM_THREADS 5

struct my_thread_info {
    long which;
};

int SharedVariable = 0;

void *SimpleThread(void *data)
{
    int num, val;
    struct my_thread_info *info = data;

    for (num = 0; num < 20; num++) {
        if (random() > RAND_MAX / 2)
            usleep(10);
        val = SharedVariable;
        printf("*** thread %ld sees value %d\n", info->which, val);
        SharedVariable = val + 1;
    }

    val = SharedVariable;
    printf("Thread %ld sees final value %d\n", info->which, val);

    free(info);
    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    struct my_thread_info *info;
    for (t = 0; t < NUM_THREADS; t++) {
        printf("In main: creating thread %ld\n", t);

        info = malloc(sizeof(struct my_thread_info));
        info->which = t;

        rc = pthread_create(&threads[t], NULL, SimpleThread, info);
        if (rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }

    for (t = 0; t < NUM_THREADS; t++) {
        pthread_join(threads[t], NULL);
    }
}
+3

All Articles