The pthread functions take a void * argument. How can a plain struct, not a pointer be sent in?
I want to send in a non pointer struct to one pthread function.
Also I want to send a pointer to the void * function, how is this done? can any pointer be sent in to the void * function?
Not possible; you have to send a pointer. However, a void *
can point to anything. If your struct variable is called foo
, you can simply pass it as (void *) &foo
, and inside the function, you can cast it back into e.g. a struct Foo
with struct Foo * fooPtr = (struct Foo *) param;
or struct Foo foo = *((struct Foo *) param);
.
Edit: As @forsvarir mentioned in the comment, foo
must not be a local variable (unless the invoking function waits for the thread to complete). See @Gavin Lock's post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With