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?
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.
pthread_create() returns zero when the call completes successfully. Any other return value indicates that an error occurred.
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.
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.
From the documentation for pthread_create
:
The thread is created executing
start_routine
witharg
as its sole argument. If thestart_routine
returns, the effect is as if there was an implicit call topthread_exit()
using the return value ofstart_routine
as the exit status. Note that the thread in whichmain()
was originally invoked differs from this. When it returns frommain()
, the effect is as if there was an implicit call toexit()
using the return value ofmain()
as the exit status.
And pthread_exit
:
The
pthread_exit()
function terminates the calling thread and makes the valuevalue_ptr
available to any successfuljoin
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.
From the spec:
If the
start_routine
returns, the effect is as if there was an implicit call topthread_exit()
using the return value ofstart_routine
as the exit status.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With