Passing function pointer to arg of pthread_create function - c

I have a function void startScanner(...) taking two function pointer as arguments: userType *vConfig(void) and void * vCallback(void). In this function i would like to create a thread and call vCallback() function in the function thread created. So i decided to pass vCallback as args to pthreadcreate.
The code of startScanner function :
void startScanner(tUsrStatus (*vConfig)(), void* (vCallback)()){
if(pthread_create(&scannerThread, NULL, scannerThreadFunc, vCallback))
{
printf("Thread creation fails!\n");
}
}
The scannerTread function:
static void *scannerThreadFunc(void *arg()){
void *funcptr(void) = arg;
while(1)
{
funcptr();
}
pthread_exit(NULL);
}
I get the following error:
error: function ‘funcptr’ is initialized like a variable
error: nested function ‘funcptr’ declared but never defined
How can i fix this?

Syntax errors aside (*) , it's impossible in standard C to pass a function pointer in a void *. There's a fundamental difference between pointers to functions and pointers to data, they can't be converted into each other. This is because there might be platforms where function and data pointers would differ even in size, or refer to different address spaces, or whatever.
But of course, there's a simple way to achieve what you want: Put your function pointer inside a struct and pass a pointer to that.
typedef (*callback)(void);
typedef struct threadargs
{
callback cb;
} threadargs;
void mycallback(void)
{
// ...
}
void *threadfunc(void *arg)
{
threadargs *ta = arg;
// call your callback:
ta->cb();
return ta; // or: return 0, or some pthread_exit(), ...
}
int main(void)
{
pthread_t thread;
threadargs ta = { mycallback };
pthread_create(&thread, 0, threadfunc, &ta);
// make sure "ta" lives for as long as the thread executes,
// here just wait until it exits:
pthread_join(&thread, 0);
}
add error checking etc.
(*) as for the concrete error you're getting, a function pointer needs parantheses around the identifier, so instead of
void *funcptr(void) = arg;
you'd have to write
void (*funcptr)(void) = arg;
To facilitate the usage of function pointers, it's common to typedef them, as seen in my example above. Anyways, as explained above, this wouldn't solve your problem here.

Related

Casting back void pointer gives different address

I'm doing this callback on linux timer, but I don't know why the address changes when it was converted back on the callback function. Code below
typedef void* timer_cb_args;
typedef void (*timer_cb)(timer_cb_args);
struct cb_wrapper
{
timer_cb callback;
timer_cb_args args;
};
void callback_wrapper(union sigval sv)
{
struct cb_wrapper *cb = (struct cb_wrapper*)(sv.sival_ptr);
printf("Casted sival_ptr pointer on Callback wrapper: %p\n\n", cb);
printf("Callback wrapper function pointer: %p\n", cb->callback);
printf("Callback wrapper args pointer: %p\n\n", &cb->args);
cb->callback(cb->args);
}
int timer_start(timer_handle_t *timer_handle,
timer_cb callback,
timer_cb_args args,
guint32 duration)
{
int ret = 0;
timer_t *timer = calloc(1, sizeof(timer_t));
*timer_handle = (timer_handle_t) calloc(1, sizeof(timer_handle_t));
(*timer_handle)->m_timer = timer;
struct sigevent evp;
memset(&evp, 0, sizeof(struct sigevent));
struct cb_wrapper cbargs;
memset(&cbargs, 0, sizeof(struct cb_wrapper));
cbargs.callback = callback;
cbargs.args = args;
evp.sigev_notify = SIGEV_THREAD;
evp.sigev_notify_function = &callback_wrapper;
evp.sigev_value.sival_ptr = &cbargs;
printf("sival_ptr pointer on call: %p\n", evp.sigev_value.sival_ptr);
printf("Function pointer: %p\n", cbargs.callback);
printf("Args pointer on call: %p\n\n", cbargs.args);
int timer_result;
timer_result = timer_create(CLOCK_REALTIME, &evp, timer);
if (timer_result < 0)
return -1;
struct itimerspec timespec;
memset(&timespec, 0, sizeof(struct itimerspec));
timespec.it_value.tv_sec = duration;
timer_result = timer_settime(*timer, 0, &timespec, NULL);
if (timer_result < 0)
return -1;
return ret;
}
output is:
sival_ptr pointer on call: 0x7ffce75c3950
Function pointer: 0x55f26d13abb4
Args pointer on call: 0x7ffce75c3a00
Callback wrapper.
Casted sival_ptr pointer on Callback wrapper: 0x7ffce75c3950 //OK same
Callback wrapper function pointer: 0x55f26d13abb4 //OK same
Callback wrapper args pointer: 0x7ffce75c3958 //NOK not same as above
The problem is here:
typedef void* timer_cb_args;
typedef void (*timer_cb)(timer_cb_args);
You are hiding pointers behind typedef and the only thing achieved by that is making everyone including yourself confused.
Therefore you write bugs such as this:
evp.sigev_notify_function = &callback_wrapper;
evp.sigev_value.sival_ptr = &cbargs;
callback_wrapper is a function pointer and they have special rules about dereferencing/decay (Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?), so that line works by accident.
cbargs is however just an ordinary void* so
evp.sigev_value.sival_ptr = &cbargs; assigned a void** to a void*. And since evp.sigev_value.sival_ptr is a void*, that's allowed without the compiler giving diagnostic messages.
This is a really subtle bug! I managed to find it in some five minutes here, but it could as well have taken forever. And the root cause is bad typedef practices.
Fix it like this:
typedef void timer_cb (timer_cb_args);
struct cb_wrapper
{
timer_cb* callback;
void* args;
};
...
evp.sigev_notify_function = callback_wrapper;
evp.sigev_value.sival_ptr = cbargs;
And then clean up the rest of the code accordingly, clearing out all pointers hidden behind typedefs.
Also unrelated to this bug, as someone pointed out it isn't a good idea to pass local variables by reference to callbacks. Because once the function setting up the callback is done, that memory is toast. A normal fix when for example passing variables to thread callbacks, is to pass a pointer to dynamic memory. Or alternatively just ensure that the thread creating thread doesn't die/go out of scope before the end of execution, after all other threads are cleaned up.

Can't get around to how to pass a function as a parameter

I'm trying to pass this function: void* checkMatrix(); as an argument to this function: void createThreads(void*(*f));.
I've read a post here so my decleration above is a result of this.
I'm calling the function like this: createThreads(checkMatrix); but it gives me a warning that type is incompatible [void** and void*()]. I can get around with a fast cast but it won't fix the problem.
Finally I write the function like this (simple initialization):
void createThreads(void* (*f)) {
pthread_t* a;
int i;
a = (pthread_t*) malloc(*arr.l * sizeof(pthread_t));
if (a == NULL) {
fprintf(stderr, "ERROR!\n");
exit(1);
}
for (i = 0; i < *arr.l; i++) {
if (pthread_create((a + i), NULL, (void*) &f, NULL)) {
fprintf(stderr, "ERROR IN THREAD CREATION!\n");
exit(2);
}
}
for (i = 0; i < *arr.l; i++)
pthread_join(*(a + i), NULL);
}
In conclusion, the problem is that it stops, with memory problem, but the cause is the creation of the threads and espacially in the 3rd argument that I specify the function that the thread will work on. I think I'm doing something wrong with the calling. I can't find the answer and can't get around it.
Thanks for your time!
void* (*f) is just void **f with a set of redundant parentheses. You probably wanted to use this for the parameter type:
void* (*f)()
However, that is not what pthread_create expects. The thread's main function is supposed to return void* and take a void* parameter. So what you really want is probably this:
void createThreads(void* (*f)(void*)) {
/* ... as before ... */
if (pthread_create((a + i), NULL, f, NULL)) {
/* ... as before ... */
}
To begin with void* checkMatrix(); is obsolete style and shouldn't be used. Second, pthread callback functions take void* as parameter. So use void* checkMatrix(void*); instead.
To pass it to a function, simply do
void createThreads (void* (*f)(void*))
Recommended practice when using function pointers is otherwise to use typedefs, to increase readability. For example you could cook up something like
typedef void* pthread_callback (void*);
void createThreads (pthread_callback* f)
Try changing the function signature to void createThreads(void* (*f)(void *))
and change the pthread_create function call topthread_create((a + i), NULL, f, NULL)

C program: pass function as parameter to another function to create thread

I would need some help. I been doing it for hours and is not able to get it to work. Generally, I have a function which access a kernel Driver and I would like to pass that function as a parameter to another function that include some pthread code. I researched and found out that I may need a function pointer.
Here is the function which I want to pass as a parameter.
static void kernelTest(char Send[BUFFER_LENGTH])
{
int fd = open("/dev/kernelTest", O_RDWR);
}
Here is the function which I want to pass in:
static void createKThread(void (*f)(char *))
{
pthread_t t1;
int ret;
ret = pthread_create(&t1, NULL, (*f)(char), NULL);
pthread_join(t1, NULL);
}
I attempted the function pointer but it is giving me error.
error: expected expression before ‘char’
I greatly appreciate any help rendered. Thank You!
(*f)(char) is invalid syntax. It looks like you're attempting to call the function f and passing char as a parameter, which you can't do.
Since you're not actually calling f, just pass it to pthread_create directly:
ret = pthread_create(&t1, NULL, f, NULL);
There's still a problem with this, however. The third parameter to pthread_create is expected to be of type void *(*)(void *), i.e. a pointer to a function that has a void * parameter and returns a void *. Your function has type void (*)(char *), so the parameters are incompatible.
You need to either change the signature of kernelTest to match what pthread_create expects:
static void *kernelTest(void *param)
{
char *send = param;
int fd = open("/dev/kernelTest", O_RDWR);
return NULL;
}
Or you need to create a wrapper function which matches pthread_create:
static void *kernelTest_wrapper(void *param)
{
char *send = param;
kernelTest(send);
return NULL;
}

Using Windows slim read/write lock

/*language C code*/
#include "windows.h"
typedef struct object_s
{
SRWLOCK lock;
int data;
} object_t, *object_p; /*own and pointer type*/
void thread(object_p x)
{
AcquireSRWLockExclusive(&x->lock);
//...do something that could probably change x->data value to 0
if(x->data==0)
free(x);
else
ReleaseSRWLockExclusive(&x->lock);
}
void main()
{
int i;
object_p object=(object_p)malloc(sizeof(object_t));
InitializeSRWLock(&object->lock);
for(i=0;i<3;i++)
CreateThread(0,0,thread,object,0);
}
As you can figure out in the codes above, what I have to accomplish is to let one thread conditionally free the object on which the other two may block. Codes above are obviously flawed because if object is set free along with the lock, all blocking threads give us nowhere but wrong.
A solution below
/*language C code*/
#include "windows.h"
typedef struct object_s
{
/*change: move lock to stack in main()*/
int data;
} object_t, *object_p; /*own and pointer type*/
void thread(void * x)
{
struct {
PSRWLOCK l;
object_p o;
} * _x=x;
AcquireSRWLockExclusive(_x->l);
//...do something that could probably change x->data value to 0
if(_x->o->data==0)
free(_x->o);
ReleaseSRWLockExclusive(&x->lock);
}
void main()
{
int i;
SRWLOCK lock; /*lock over here*/
object_p object=(object_p)malloc(sizeof(object_t));
InitializeSRWLock(&lock);
/*pack for thread context*/
struct
{
PSRWLOCK l;
object_p o;
} context={&lock, object};
for(i=0;i<3;i++)
CreateThread(0,0,thread,&context,0);
}
works in this case but not applicable however, in my final project because there is actually a dynamic linked list of objects. By applying this solution it means that there must be a list of locks accordingly, each lock for an object and moreover, when a certain object is set free, its lock must be set free at the same time. There is nothing new compared with the first code section.
Now I wonder if there is an alternative solution to this. Thank you very much!
The solution is to not allocate the lock together with the data. I would suggest that you move the data out of that struct and replace it with a pointer to the data. Your linked list can then free the data first, and then the node, without any problems. Here's some pseudo code:
typedef struct
{
lock_t lock;
int* data_ptr;
} something_t;
void init_something (something_t* thing, ...)
{
thing->lock = init_lock();
thing->data_ptr = malloc(...); // whatever the data is supposed to be
}
void free_something (somthing_t* thing)
{
lock(thing->lock);
free(thing->data_ptr);
thing->data_ptr = NULL;
unlock(thing->lock);
}
...
void linked_list_delete_node (...)
{
free_something(node_to_delete->thing);
free(node_to_delete);
}
...
void thread (void* x)
{
lock(x->lock);
//...do something that could probably change x->data_ptr->data... to 0
if(x->data_ptr->data == 0)
{
free_something(x->data_ptr->data);
}
unlock(x->lock);
}
AcquireSRWLockExclusive(lock);
if(_x->o->data==0)
free(_x);
ReleaseSRWLockExclusive(lock);
As a sidenote, a C program for Windows can never return void. A hosted C program must always return int. Your program will not compile on a C compiler.
Also, CreateThread() expects a function pointer to a function returning a 32-bit value and taking a void pointer as parameter. You pass a different kind of function pointer, function pointer casts aren't allowed in C, nor am I sure what sort of madness Windows will execute if it gets a different function pointer than what it expects. You invoke undefined behavior. This can cause your program to crash or behave in unexpected or random ways.
You need to change your thread function to DWORD WINAPI thread (LPVOID param);

Implementing callback functions in C

I am a newbie to C. I am trying to implement callback function using function pointers.
I am getting an error
:test_callback.c:10: error: expected identifier or ‘(’ before ‘void’
when I try to compile the following program:
#include<stdio.h>
void (*callback) (void);
void callback_proc ()
{
printf ("Inside callback function\n");
}
void register ((void (*callback) (void)))
{
printf ("Inside registration \n");
callback (); /* Calling an initial callback with function pointer */
}
int main ()
{
callback = callback_proc;/* Assigning function to the function pointer */
register (callback);/* Passing the function pointer */
return 0;
}
What is this error?Can anyone help?
register is a C keyword: Use another name for the function.
You have extra parantheses around the callback parameter. It should be:
void funcName(void (*callback) (void))
I would recommend to use a typedef
#include<stdio.h>
typedef void (*callback_t) (void);
callback_t callback;
void callback_proc(void)
{
printf ("Inside callback function\n");
}
void reg( callback_t _callback )
{
printf ("Inside registration \n");
_callback();
}
int main ()
{
callback = callback_proc;
reg(callback);
return 0;
}
EDIT: removed the register issue
You can't use 'register' as a function name as it's a C keyword.
2 problems:
you can't use the name register as it's a keyword (not used often anymore, but it's still there)
change the definition of the function from
void wasRegister((void (*callback) (void)))
to:
void wasRegister(void (*callback) (void))
(get rid of the parens around the parameter's declaration.
Also you might get a warning about callback_proc() not having a matching delaration to the callback variable (depending on how you compile the program - as C or C++), so you might want to change its declaration to:
void callback_proc (void)
to make it explicit that it takes no parameters.
Have a look at type safe callbacks from ccan. Its one thing to expose a typed function pointer for the world to use, its another to ensure sane casting.
#include<stdio.h>
typedef void (*callback_func) (void);
static callback_func the_callback = 0;
void process (void)
{
printf ("Inside process function\n");
}
void callback_register (callback_func cb)
{
the_callback = cb;
printf ("Inside registration \n");
}
void callback(void)
{
the_callback();
}
int main (void)
{
callback_register(process); /* Passing the function pointer */
callback();
return 0;
}
Declaring the_callback static would make more sense if this code was modularized and then you would be forced to call callback_register in order to set it, and callback in order to call it - the_callback would not be accessible outside of the implementation (.c) only the function declarations would be in the header (.h).

Resources