-1

so i'm diving into multithreading in c via POSIX pthreads but i do really struggle with the general concept of pointers and their referencing and dereferencing mechanisms. One of the parameters in pthread_create(...,pthread_attr_t *attr,...) is a function pointer.

This function is usually declared like this:

void *thr_func(void *arg){ thread_data_t *data = (thread_data_t *)arg; ... }

thr_func is a function pointer, so usually i use a function pointer to reference to an existing, implemented function via &, e.g.:

thr_func = &thr_func_impl;

while the arguments of thr_func are also pointers dereferenced for example via * to retrieve the values they are pointing to.

What I don't understand is the following:

  • when i create a thread, why do i just give the function name in pthread_create(...,thr_func,...) instead of its address so that it can be used, e.g.: pthread_create(...,&thr_func,...) or is this done by pthread_create() already ?

  • How do i have to understand this part: thread_data_t *data = (thread_data_t *)arg; okay i want to dereference a structure of type thread_data called data via thread_data_t *data = ... . Shouldn't i do it this way: thread_data_t *data; data = &arg; /* now * on data ,e.g.: *data == struct-data (dereferencing) gives the struct data and data without * just gives the structs start address */

-> I cannot really follow the things happening inside:

void *thr_func(void *arg){ thread_data_t *data = (thread_data_t *)arg; ... }

I'd be glad if somebody had a good explanation, thanks!

2 Answers 2

1
  1. Passing the function name as a parameter is equivalent to passing its address using the & operator. I.e. pthread_create(...,thr_func,...) and pthread_create(...,&thr_func,...) are interchangeable. This has nothing to do with pthreads, it is a language construct.
  2. arg is a pointer of type void. In order to cast it to a pointer of type thread_data_t you should do (thread_data_t *)arg. By using thread_data_t *data = &arg you are attributing the address of arg itself to data, not the address arg is pointing to.

Consider the following:

int my_int = 7;
void *my_void_ptr = &my_int;

int *my_int_ptr = (int *)my_void_ptr; // Just a cast, points to my_int
printf("%d", *my_int_ptr);            // 7
int *my_int_ptr2 = &my_void_ptr;      // int pointer to the address of my_void_ptr, issues a warning
printf("%d", *my_int_ptr2 );          // The address of my_void_ptr as an integer

Finally, pthreads stands for POSIX threads, so "POSIX pthreads" is redundant.

2
  • Okay, I think I got it. I didn't know that the function name & the address-operator applied to it are interchangeable, but it makes sense since the only thing we're interested in might be the start position of a function in the address space of the program. In your explanation void *my_void_ptr = &my_int simply declares a void pointer to my_int.
    – Leo
    Jul 11, 2018 at 9:46
  • [Continuation of @Leo's previous comment:] Since this pointer has not yet been assigned a type, we need to cast it using: ...= (int *) my_void_ptr; & assign it to f.e. int *my__int_ptr = (int *) my_void_ptr; in order to use it inside the function which is also just a pointer now pointing to the same address as my_void_ptr. Now having had declared my_int_ptr as a pointer, see: int *my_int_ptr =...;, we can simply dereference and get its value via ....*my_int_ptr; and get the address of the stored value (my_int/7 in your example) by ...&(*my_int_ptr);.
    – elixenide
    Jul 11, 2018 at 18:09
0

For the first question, a function will naturally decay to a pointer to itself, similar to the way that arrays decays to a pointer to its first element.

So if you have a function

void *thr_fun(void *arg) { ... }

then you can pass a pointer to it using either &thr_fun which is the explicit and (some would say) "correct" way. The other is to use plain thr_fun as it will decay to &thr_fun anyway. So in C it's really a question about style and personal preference.


For your second question, remember that void * is a generic pointer that can point to anything but it doesn't have any explicit type. Therefore you can't dereference it (since a dereferenced void * would have the type void).

That's the reason you need to cast it.

And no you should not get the address of the pointer, since that will give you a pointer to the pointer.

1
  • And to counter the "correct" way, I'd say that the best way is to use the shortest construct allowed by the language which is still correct. Jul 11, 2018 at 9:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.