I’m trying to understand some code I’ve been given. It works in a similar way to pthread_create: you pass it a pointer to a function start_routine which takes a pointer to void arg as the single argument, and the argument pointer itself, and then pthread_create calls your function.
Generally, if start_routine actually needs an argument, it does not need a void *, but rather arg should be cast to a pointer to some struct. In the past, I’ve coded my start_routines like this:
void *my_start_routine(void *arg)
{
struct my_struct *p = arg;
// use p many times
}
However, I just received code where the author sets up a macro to do the cast every time p is used:
void *my_start_routine(void *arg)
{
#define p ((struct my_struct *) arg)
// use p many times
#undef p
}
I’m having a hard time understanding why you’d want to do this. But I assume it is deliberate, because it seems more difficult and less obvious than the first way, and the author uses similar idioms throughout the code. Is there some reason, performance or otherwise, to prefer the macro here?
Your idiom is preferable; the other person’s with the macro is, in my opinion, an abomination, for reasons like:
pis not an lvalueargaffectp.pis.