The question I have is, what exactly is the format of the pthread_create function and the function it calls, in terms of pointers and such? I can wrap my head around variable pointers, although I need to clarify my knowledge in that area, but function pointers get icky.
I understand the preferred format is
void *threadfunc(void *arg);
int main()
{
pthread_t threadinfo;
int samplearg;
pthread_create(&threadinfo, NULL, threadfunc, &samplearg);
}
However, this generates a compiler warning that threadfunc's not returning a value, so apparently the * is something about what threadfunc returns, not a characteristic of the function?
I have also seen the function defined as and the pthread_create formatted like such:
void threadfunc(void *arg);
pthread_create(&threadinfo, NULL, (void *)threadfunc, &samplearg);
Which of these two are correct, or are they functionally equivalent? Could someone explain to me the mechanics of pointers to functions and such?
One last question, would it work to, inside a for loop generating multiple threads, initialize an int samplearg for thread unique values then pass it to pthread_create(...)? I understand samplearg would be in scope inside the threadfunc, I'm just checking in case somehow C doesn't follow typical scope rules--since samplearg is created inside the for() loop and would typically go out of scope after the iteration of the for() loop, and the actual variable itself is being passed not the value. I'd test myself but there might be a point you could enlighten me on, and developing on a remote linux machine is a bit cumbersome for me.
pthread_create()