Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does start_routine for pthread_create return void* and take void*

Tags:

c

posix

pthreads

The function header for pthread_create looks like this:

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

I understand it all except that the function pointer for start_routine is of the form void* (*fpointer) (void*) which means it takes in a void* and returns a void*.

The void* parameter is just a way to pass in an argument to the start_routine, I get that part, but I don't understand why the function returns a void*? What code will even notice that return value?

like image 204
ldog Avatar asked Aug 29 '09 21:08

ldog


People also ask

What does pthread_create function return?

The pthread_create() imposes a strict format on the prototype of the function that will run in the new thread. It must take a single void* parameter and return a single void* value.

What is the return type of pthread_create?

pthread_create() returns zero when the call completes successfully. Any other return value indicates that an error occurred.

What happens when pthread_create is called?

If pthread_create() completes successfully, thread will contain the ID of the created thread. If it fails, no new thread is created, and the contents of the location referenced by thread are undefined. System default for the thread limit in a process is set by MAXTHREADS in the BPXPRMxx parmlib member.

What causes pthread_create to fail?

The pthread_create() function will fail if: [EAGAIN] The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.


2 Answers

From the documentation for pthread_create:

The thread is created executing start_routine with arg as its sole argument. If the start_routine returns, the effect is as if there was an implicit call to pthread_exit() using the return value of start_routine as the exit status. Note that the thread in which main() was originally invoked differs from this. When it returns from main(), the effect is as if there was an implicit call to exit() using the return value of main() as the exit status.

And pthread_exit:

The pthread_exit() function terminates the calling thread and makes the value value_ptr available to any successful join with the terminating thread.

So if you do a pthread_join on a thread, the pointer it returns is passed back to the joining thread, allowing you to transmit information from the dying thread to another, living thread.

like image 189
bcat Avatar answered Oct 12 '22 04:10

bcat


From the spec:

If the start_routine returns, the effect is as if there was an implicit call to pthread_exit() using the return value of start_routine as the exit status.

like image 44
Martin v. Löwis Avatar answered Oct 12 '22 03:10

Martin v. Löwis