[C] Array of function pointers
- M.R
- Jul 27, 2024
- 1 min read
What want to do
As stated in the title, I would like to define an array of function pointers.
Specifically, for example, the third argument of pthread_create is the pointer to the function you want to run in the new thread. When you want to allocate new threads for multiple functions, you can put those pointers into an array and process them together.
Solution
You can do it like this:
void *(*funcs[])(void *)
The meaning is as follows:
(*funcs): function pointers
(*funcs)(void *): A pointer to a function that takes a void pointer as an argument.
void *( *funcs): A pointer to a function that returns void *
Use it like this
void *(*services[])(void *) = {Service_1, Service_2, Service_3};
for (i = 0; i< NUM_THREADS; i++){
pthread_create(&threads[0], sched_attr, services[i], (void *)threadParams);
}
void *Service_1(void *threadp){
...
}
Recent Posts
See AllWhat want to do There is an operation that operates on two-dimensional arrays There are multiple targets to be processed In this case, I...
Phenomenon Create a function (func_a) in C language. Creates an instance of a struct based on the contents of the argument and returns...
コメント