1
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

I am trying to pass in an argument as a pointer through pthread_create.

typedef struct {
    int input;
    int threadNum;
} ARGS;

ARGS argument16[16];
for (x = 0; x < 16; x++) {
    printf("pthread16_created: %d\n", x);
    argument16[x].threadNum = 16;
    argument16[x].input = x % 4;
    pthread_create(&(threads16[x]), NULL, funct, argument16[x]);
}

This is giving me error: incompatible type for argument 4 of ‘pthread_create’

How do I pass in a ARG type pointer in a array of ARGS? I can't seem to figure it out. I know arrays are already a pointer, but not sure how to pointer to a certain element in the array. Thank you

2
  • &argument16[x] In other words, you want to pass the address of the element, not the element itself. Commented Nov 11, 2015 at 2:32
  • Oh Thanks! I don't know why I didn't think about that Commented Nov 11, 2015 at 2:42

1 Answer 1

3

The final argument to pthread_create() is a pointer to void so an address typecast to void * is expected. So, like commenter said, the address of argument16[x] rather than the value which is what you passed. Otherwise known as rvalue.

Inside the thread function, you want a pointer of correct type so:

ARGS * ptr  = (   ARGS *  ) arg;

Is the expected way to type cast back.

If you aren't sure in the future use a pointer to same type and increment it per loop then pass by lvalue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.