Create a thread
int pthread_create( pthread_t* thread,
const pthread_attr_t* attr,
void* (*start_routine)(void* ),
void* arg );
- NULL, or a pointer to a pthread_t
object where the function can store the thread ID of the new thread.
- A pointer to a pthread_attr_t structure that specifies
the attributes of the new thread.
Instead of manipulating the members of this structure directly, use
and the pthread_attr_set_* functions.
For the exceptions, see
If attr is NULL, the default attributes are used
|| If you modify the attributes in attr after creating the thread,
the thread's attributes aren't affected.
- The routine where the thread begins, with arg as its only
If start_routine() returns, there's an implicit call to
using the return value of start_routine() as the exit status.
The thread in which main() was invoked behaves differently.
When it returns from main(), there's an implicit call to
using the return value of main() as the exit status.
- The argument to pass to start_routine.
Use the -l c option to
to link against this library.
This library is usually included automatically.
The pthread_create() function creates a new thread, with the
attributes specified in the thread attribute object attr.
The created thread inherits the signal mask of the parent thread, and
its set of pending signals is empty.
- You must call
for threads created with a detachstate attribute of
PTHREAD_CREATE_JOINABLE (the default) before all of the
resources associated with the thread can be released at
- If you set the __stacksize member of attr,
the thread's actual stack size is rounded up to a multiple of the
system page size (which you can get by using the _SC_PAGESIZE
constant in a call to
sysconf()) if the
system allocates the stack (the __stackaddr member of
attr is set to NULL). If the stack was
previously allocated by the application, its size isn't
If you adhere to the POSIX standard,
there are some thread attributes that you can't
specify before creating the thread:
- You can't disable cancellation for a thread.
- You can't set the thread's cancellation type.
- You can't specify what happens when a signal is delivered to the thread.
There are no pthread_attr_set_* functions for these
As an QNX extension, you can OR the following bits into the __flags
member of the pthread_attr_t structure before calling
- Cancellation requests may be acted on according to the cancellation type
- Cancellation requests are held pending.
- If cancellation is enabled, new or pending cancellation requests
may be acted on immediately.
- If cancellation is enabled, cancellation requests are held pending
until a cancellation point (the default).
- Terminate all the threads in the process (the POSIX default).
- Terminate only the thread that received the signal.
After creating the thread, you can change the cancellation properties
- Insufficient system resources to create thread.
- An error occurred trying to access the buffers or the
- Invalid thread attribute object attr.
Create a thread in a detached state:
void* function( void* arg )
printf( "This is thread %d\n", pthread_self() );
return( 0 );
int main( void )
pthread_attr_init( &attr );
&attr, PTHREAD_CREATE_DETACHED );
pthread_create( NULL, &attr, &function, NULL );
/* Allow threads to run for 60 seconds. */
sleep( 60 );
POSIX 1003.1 THR
Processes and Threads
chapter of Getting Started with QNX Neutrino