This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
Create a thread
#include <pthread.h> int pthread_create( pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void* ), void* arg );
If attr is NULL, the default attributes are used (see pthread_attr_init()).
If you modify the attributes in attr after creating the thread, the thread's attributes aren't affected. |
The thread in which main() was invoked behaves differently. When it returns from main(), there's an implicit call to exit(), using the return value of main() as the exit status.
libc
Use the -l c option to qcc 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.
|
If you adhere to the POSIX standard, there are some thread attributes that you can't specify before creating the thread:
There are no pthread_attr_set_* functions for these attributes.
As an QNX extension, you can OR the following bits into the flags member of the pthread_attr_t structure before calling pthread_create():
After creating the thread, you can change the cancellation properties by calling pthread_setcancelstate() and pthread_setcanceltype().
Create a thread in a detached state:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void* function( void* arg ) { printf( "This is thread %d\n", pthread_self() ); return( 0 ); } int main( void ) { pthread_attr_t attr; pthread_attr_init( &attr ); pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); pthread_create( NULL, &attr, &function, NULL ); /* Allow threads to run for 60 seconds. */ sleep( 60 ); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
pthread_attr_init(), pthread_exit(), pthread_setcancelstate(), pthread_setcanceltype(), sysconf(), ThreadCreate()