pthread_create |
Prototype: |
#include <pthread.h>
int pthread_create(pthread_t *tchild, pthread_attr_t *attr,
void (*start_fn)(void *), void *arg);
|
General Description: |
This call creates a light-weight kernel process (thread). The thread starts in the function that start_fn points to using arg as the function's parameter. When the function returns, the thread terminates. The function should return a void* value, but if it doesn't, the thread still terminates and the result is set to NULL. |
Return Value: |
A positive value if successful. If the thread-create call encountered any errors, the call returns a negative value and sets errno to the error. |
Parameters |
thread |
The thread handle (passed by reference). If successful, the call places the thread handle in this parameter. |
attr |
The thread's starting attributes. See pthread_attr_init for more information. |
start_fn |
The routine that the thread is to start in. This function should return a void* value. |
arg |
The parameter passed to start_fn. You should make this parameter a nonshared (unless you plan on locking it), nonstack memory reference. |
Possible Errors |
EAGAIN |
|
|
Not enough system resources to create a process for the new thread. |
EAGAIN |
More than PTHREAD_THREADS_MAX threads are already active. |
Examples |
void* child(void *arg)
{
/**** Do something! ****/
pthread_exit(arg); /* terminate and return arg */
}
int main()
{ pthread_t tchild;
if ( pthread_create(&tchild, 0, child, 0) < 0 )
perror("Can't create thread!");
/**** Do something! ****/
if ( pthread_join(tchild, 0) != 0 )
perror("Join failed");
}
|