#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
The pthread_create() function creates a thread with the specified attributes and runs the C function start_routine in the thread with the single pointer argument specified. The new thread may, but does not always, begin running before pthread_create() returns. If pthread_create() completes successfully, the Pthread handle is stored in the contents of the location referred to by thread.
If the start_routine returns normally, it is as if there was an implicit call to pthread_exit() using the return value of start_routine as the status. The function passed as start_routine should correspond to the following C function prototype:
void *threadStartRoutinName(void *);
If the thread attributes object represented by attr is modified later, the newly created thread is not affected. If attr is NULL, the default thread attributes are used.
With the following declarations and initialization,
pthread_t t; void *foo(void *); pthread_attr_t attr; pthread_attr_init(&pta);
the following two thread creation mechanisms are functionally equivalent:
rc = pthread_create(&t, NULL, foo, NULL); rc = pthread_create(&t, &attr, foo, NULL);
The cancellation state of the new thread is PTHREAD_CANCEL_ENABLE. The cancellation type of the new thread is PTHREAD_CANCEL_DEFERRED.
The signal information maintained in the new thread is as follows:
If you attempt to create a thread in a job that is not capable of starting threads, pthread_create() fails with the EBUSY error. If you attempt to create a thread from a location in which thread creation is not allowed, pthread_create() fails with the EBUSY error. See the pthread_getpthreadoption_np() function, option PTHREAD_OPTION_THREAD_CAPABLE_NP, for details about how to determine whether thread creation is currently allowed in your process.
In the i5/OS implementation, the initial thread is special. Termination of the initial thread by pthread_exit() or any other thread termination mechanism terminates the entire process.
The i5/OS implementation does not set a hard limit on the number of threads that can be created. The PTHREAD_THREADS_MAX macro is implemented as a function call, and returns different values depending on the administrative setting of the maximum number of threads for the process. The default is NO MAX and has the numeric value of 2147483647 (0x7FFFFFFF). Realistically, the number of threads is limited by the amount of storage available to the job.
Currently, thread creation is not allowed after process termination has been started. For example, after a call to exit(), destructors for C++ static objects, functions registered with atexit() or CEE4RAGE() are allowed to run. If these functions attempt to create a thread, pthread_create() fails with the EBUSY error. Similar failures occur if other mechanisms are used to call pthread_create() after process termination has started.
None.
If pthread_create() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.
The value specified for the argument is not correct.
The system did not have enough resources to create another thread or the maximum number of threads for this job has been reached.
The system cannot allow thread creation in this process at this time.
See Code disclaimer information for information pertaining to code examples.
#define _MULTI_THREADED #include <pthread.h> #include <stdio.h> #include "check.h" typedef struct { int value; char string[128]; } thread_parm_t; void *threadfunc(void *parm) { thread_parm_t *p = (thread_parm_t *)parm; printf("%s, parm = %d\n", p->string, p->value); free(p); return NULL; } int main(int argc, char **argv) { pthread_t thread; int rc=0; pthread_attr_t pta; thread_parm_t *parm=NULL; printf("Enter Testcase - %s\n", argv[0]); printf("Create a thread attributes object\n"); rc = pthread_attr_init(&pta); checkResults("pthread_attr_init()\n", rc); /* Create 2 threads using default attributes in different ways */ printf("Create thread using the NULL attributes\n"); /* Set up multiple parameters to pass to the thread */ parm = malloc(sizeof(thread_parm_t)); parm->value = 5; strcpy(parm->string, "Inside secondary thread"); rc = pthread_create(&thread, NULL, threadfunc, (void *)parm); checkResults("pthread_create(NULL)\n", rc); printf("Create thread using the default attributes\n"); /* Set up multiple parameters to pass to the thread */ parm = malloc(sizeof(thread_parm_t)); parm->value = 77; strcpy(parm->string, "Inside secondary thread"); rc = pthread_create(&thread, &pta, threadfunc, (void *)parm); checkResults("pthread_create(&pta)\n", rc); printf("Destroy thread attributes object\n"); rc = pthread_attr_destroy(&pta); checkResults("pthread_attr_destroy()\n", rc); /* sleep() is not a very robust way to wait for the thread */ sleep(5); printf("Main completed\n"); return 0; }
Output:
Enter Testcase - QP0WTEST/TPCRT0 Create a thread attributes object Create thread using the NULL attributes Create thread using the default attributes Destroy thread attributes object Inside secondary thread, parm = 77 Inside secondary thread, parm = 5 Main completed
Top | Pthread APIs | APIs by category |