1

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.

3
  • pthread_create( ) returns an int. From the man page: RETURN VALUE If successful, the pthread_create() function shall return zero; otherwise, an error number shall be returned to indicate the error. Commented Oct 26, 2011 at 16:00
  • @PeteWilson - he's not talking about the return value of pthread_create() Commented Oct 26, 2011 at 16:02
  • @Brian Roach -- so he's not! thanks for the pickup. Commented Oct 26, 2011 at 19:14

3 Answers 3

2

You have not given your version of void *threadfunc(void *arg); but I am guessing that there is no return statement in it. This is why the compiler is warning you. Since the declaration says that it must return a void* you should return a void*. A void* is a pointer to any pointer type. Only void (without the star) does not need a return statement because it is returning nothing.

Incidentally, the return value will be passed to a pthread_join statement when another thread joins to the thread you are currently starting.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I didn't have a return value since I misunderstood void *threadfunc(void *arg); as being a function with a void return value and a special property indicated by the *. What would a suitable "blank" return be? Or should the return be something else? Of course pthread_exit() would be called in threadfunc() itself.
If there is nothing that you want to return to the joining thread, just return NULL.
I think I get the purpose of the pointer to void return now--it's to pass a variable that will then be cast, similar to how the input arg is passed, right?
1

Go to the 'source' - the POSIX standard. For pthread_create(), it says:

int pthread_create(pthread_t *restrict thread,
   const pthread_attr_t *restrict attr,
   void *(*start_routine)(void*), void *restrict arg);

That is, your 'start routine' must be a function that returns a void * and which takes a void * argument.

void *possible_thread_start_routine(void *data)
{
    SomeStruct *info = data;
    ...main code for thread...
    return 0;
}

The argument passed to the thread start routine is the one specified in arg to pthread_create().

1 Comment

What does the pointer inside the parentheses around start_routine indicate? I.e. (*start_routine)
0

The function passed to pthread_create() should return a void pointer that points to the exit status of the thread.

That return value is available to you when you call pthread_join() on the thread after it exits (either by returning or explicitly calling pthread_exit())

1 Comment

So you're saying then that the first formulation, with the function definition void *threadfunc(void *arg); is correct? What's the purpose of returning the pointer to void? Being able to re-enter the function with the pointer retrieved from pthread_join()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.