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 bypthread_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 viathread_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!