Reputation: 83
I am having problems doing the following scenario. I have a function which comes as:
void testing(mystruct str**) {
pthread threads[5];
for(int i = 0; i < 5; i++) {
pthread_create(&thread[i], NULL, process, (void)&(*str)); //not really sure how to pass this here
}
}
void * process(void *arg) {
struct mystruct **value = arg; //probably wrong but it compiles, then blows up some where else because its not really the value that was original passed.
}
I am new to C and as far as I understand this is whats its doing,
to pass it to the fuction I would pass it casted as void because thats what the function accepts and also with the address of the str pointer (or would it be the address of the pointer to pointer? &str ?) *then when obtaining it on the other side, I set it, i tried casting it to the struct but that did not go well either as such:
struct mystruct **value = *(mystruct *)arg;
but i get "dereferencing pointer to incomplete type"
any help understanding this would be much appreciated.
Upvotes: 0
Views: 104
Reputation: 213513
You should be able to pass on the pointer as-is, since every object pointer type in C can be implicitly converted to/from void*
. Still, it is more correct to de-reference the pointer one level. That is:
pthread_create(&thread[i], NULL, process, *str);
IMPORTANT: This assumes that str
points at a variable which will not go out of scope while the thread is executing! Passing on pointers to local variables to threads is a common source of bugs.
From there on, your thread callback should do something like this:
void * process(void *arg) {
struct mystruct* ms = arg;
struct mystruct** ptr_ms = &arg;
However, the error "dereferencing pointer to incomplete type" simply means that the struct definition isn't visible to the thread, causing the struct type to get treated as incomplete. So the issue might as well be a missing #include
.
Upvotes: 2