#include <pthread.h>
Link against:
libpthread.lib
POSIX_THREAD_DESTRUCTOR_ITERATIONS
POSIX_THREAD_DESTRUCTOR_ITERATIONS 4
Description
Controlling the iterations of destructors for thread-specific data.
| Interface status: |
externallyDefinedApi |
|
POSIX_THREAD_KEYS_MAX 128
Description
Represents maximum value
| Interface status: |
externallyDefinedApi |
|
PTHREAD_STACK_MIN 0x2000
Description
Minimum supported stack size for a thread
| Interface status: |
externallyDefinedApi |
|
POSIX_THREAD_THREADS_MAX 64
Description
Thread max. Value is 64
| Interface status: |
externallyDefinedApi |
|
PTHREAD_THREADS_MAX 1024
Description
Maximum number of threads supported per process. Value is 1024
POSIX_SEM_NSEMS_MAX 256
Description
Number of semaphores a process can have.
SEM_NSEMS_MAX 1024
Description
The maximum number of semaphores a process can value is 1024
| Interface status: |
externallyDefinedApi |
|
SEM_VALUE_MAX INT_MAX
Description
Semaphores have a maximum value past which they cannot be incremented. The macro SEM_VALUE_MAX is defined to be this maximum
value.
| Interface status: |
externallyDefinedApi |
|
PTHREAD_COND_INITIALIZER { _ENeedsNormalInit, NULL,NULL }
Description
Value is 4. Describes condition initializer.
| Interface status: |
externallyDefinedApi |
|
typedef int pthread_once_t;
Description
Used for dynamic package initialization.
Typedef pthread_condattr_t
| Interface status: |
externallyDefinedApi |
|
typedef struct _pthread_condattr_t * pthread_condattr_t;
Description
Used to identify a condition attribute object
pthread_create(pthread_t *,pthread_attr_t *,thread_begin_routine,void *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_create(pthread_t *threadhdl, pthread_attr_t *attrib, thread_begin_routine begin_routine, void *param);
Description
The pthread_create function is used to create a new thread, with attributes specified by attrib , within a process. If attrib
is NULL , the default attributes are used. If the attributes specified by attrib are modified later, the thread's attributes
are not affected. Upon successful completion pthread_create will store the ID of the created thread in the location specified
by threadhdl .
The thread is created executing begin_routine with param as its sole argument. If the begin_routine returns, the effect is
as if there was an implicit call to pthread_exit using the return value of start_routine as the exit status. Note that the
thread in which main was originally invoked differs from this. When it returns from main , the effect is as if there was an
implicit call to exit using the return value of main as the exit status.
Examples:
void *a_thread_func_1_1(void*)
{
return NULL;
}
int XYZ()
{
pthread_t new_th;
if(pthread_create(&new;_th, NULL, a_thread_func_1_1, NULL) != 0)
{
perror("Error creating thread
");
return -1;
}
}
Parameters
pthread_t *threadhdl |
|
pthread_attr_t *attrib |
|
thread_begin_routine begin_routine |
|
void *param |
|
|
Return value
int |
If successful, the pthread_create function will return zero. Otherwise an error number will be returned to indicate the error.
|
|
See also:
| Interface status: |
externallyDefinedApi |
|
IMPORT_C pthread_t pthread_self();
Description
The pthread_self function returns the thread ID of the calling thread.
Examples:
pthread_t new_th2;
new_th2=pthread_self();
Return value
pthread_t
|
The pthread_self function returns the thread ID of the calling thread.
|
|
See also:
pthread_equal(pthread_t,pthread_t)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_equal(pthread_t t1, pthread_t t2);
Description
The pthread_equal function compares the thread IDs t1 and t2 .
Examples:
void *a_thread_func_1_2(void*)
{
pthread_exit(0);
return NULL;
}
void XYZ()
{
pthread_t new_th1, new_th2;
/* Create a new thread. */
if(pthread_create(&new;_th1, NULL, a_thread_func_1_2, NULL) != 0)
{
perror("Error creating thread
");
return -1;
}
/* Create another new thread. */
if(pthread_create(&new;_th2, NULL, a_thread_func_1_2, NULL) != 0)
{
perror("Error creating thread
");
return -1;
}
/* Call pthread_equal() and pass to it the 2 new threads.
It should return a zero value, indicating that
they are not equal. */
if(pthread_equal(new_th1, new_th2) != 0)
{
printf("pthread_equal FAILED
");
return -1;
}
}
Parameters
pthread_t t1 |
|
pthread_t t2 |
|
|
Return value
int |
The pthread_equal function will return non-zero if the thread IDs t1 and t2 correspond to the same thread, otherwise it will
return zero.
|
|
See also:
pthread_join(pthread_t,void **)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_join(pthread_t thHandle, void **retValPtr);
Description
The pthread_join function suspends execution of the calling thread until the target thrHandle terminates unless the target
thrHandle has already terminated.
On return from a successful pthread_join call with a non-NULL retValPtr argument, the value passed to pthread_exit by the
terminating thread is stored in the location referenced by retValPtr . When a pthread_join returns successfully, the target
thread has been terminated. The results of multiple simultaneous calls to pthread_join specifying the same target thread are
undefined.
Examples:
/* Thread's function. */
void *a_thread_func_1_1(void*)
{
int i;
printf("Wait for 3 seconds for thread to finish execution:0);
for(i=1;i<4;i++)
{
printf("Waited (%d) second0, i);
sleep(1);
}
return NULL;
}
int XYZ()
{
pthread_t new_th;
/* Create a new thread. */
if(pthread_create(&new;_th, NULL, a_thread_func_1_1, NULL) != 0)
{
perror("Error creating thread
");
return -1;
}
/* Wait for thread to return */
if(pthread_join(new_th, NULL) != 0)
{
perror("Error in pthread_join()
");
return -1;
}
Parameters
pthread_t thHandle |
|
void **retValPtr |
|
|
Return value
int |
If successful, the pthread_join function will return zero. Otherwise an error number will be returned to indicate the error.
|
|
See also:
pthread_detach(pthread_t)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_detach(pthread_t thrHandle);
Description
The pthread_detach function is used to indicate to the implementation that storage for the thread thrHandle can be reclaimed
when the thread terminates. If thrHandle has not terminated, pthread_detach will not cause it to terminate. The effect of
multiple pthread_detach calls on the same target thread is unspecified.
Examples:
void *a_thread_func_1_1(void*)
{
sleep(10);
return NULL;
}
int main_1_1()
{
pthread_attr_t new_attr;
pthread_t new_th;
int ret;
/* Initialize attribute */
if(pthread_attr_init(&new;_attr) != 0)
{
perror("Cannot initialize attribute object
");
return -1;
}
/* Set the attribute object to be joinable */
if(pthread_attr_setdetachstate(&new;_attr, PTHREAD_CREATE_JOINABLE) != 0)
{
perror("Error in pthread_attr_setdetachstate()
");
return -1;
}
/* Create the thread */
if(pthread_create(&new;_th, &new;_attr, a_thread_func_1_1, NULL) != 0)
{
perror("Error creating thread
");
return -1;
}
/* Detach the thread. */
if(pthread_detach(new_th) != 0)
{
printf("Error detaching thread
");
return -1;
}
Parameters
Return value
int |
If successful, the pthread_detach function will return zero. Otherwise an error number will be returned to indicate the error.
|
|
See also:
| Interface status: |
externallyDefinedApi |
|
IMPORT_C void pthread_exit(void *retValPtr);
Description
The pthread_exit function terminates the calling thread and makes the value retValPtr available to any successful join with
the terminating thread. Thread termination does not release any application visible process resources, including, but not
limited to, mutexes and file descriptors, nor does it perform any process level cleanup actions, including, but not limited
to, calling atexit routines that may exist.
An implicit call to pthread_exit is made when a thread other than the thread in which main was first invoked returns from
the start routine that was used to create it. The function's return value serves as the thread's exit status.
The behavior of pthread_exit is undefined if called from a cancellation handler or destructor function that was invoked as
the result of an implicit or explicit call to pthread_exit .
After a thread has terminated, the result of access to local (auto) variables of the thread is undefined. Thus, references
to local variables of the exiting thread should not be used for the pthread_exit retValPtr parameter value.
The process will exit with an exit status of 0 after the last thread has been terminated. The behavior is as if the implementation
called exit with a zero argument at thread termination time.
Examples:
/* Thread's function. */
void *a_thread_func_1_1(void*)
{
/* .. Do some thing .. */
pthread_exit((void*)RETURN_CODE);
}
Parameters
See also:
pthread_attr_init(pthread_attr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_init(pthread_attr_t *attrib);
Description
Thread attributes are used to specify parameters to pthread_create . One attribute object can be used in multiple calls to
pthread_create , with or without modifications between calls.
The pthread_attr_init function initializes attrib with all the default thread attributes.
The pthread_attr_destroy function destroys attrib .
The pthread_attr_set* functions set the attribute that corresponds to each function name.
The pthread_attr_get* functions copy the value of the attribute that corresponds to each function name to the location pointed
to by the second function parameter.
Examples:
/* ***************************************************** */
/* Example for pthread_attr_init & pthread_attr_destroy */
/* ***************************************************** */
pthread_attr_t new_attr;
/* Initialize attribute */
if(pthread_attr_init(&new;_attr) != 0)
{
printf("Cannot initialize attribute object
");
return -1; /* or exit */
}
/* Create thread */
/* Destroy attribute */
if(pthread_attr_destroy(&new;_attr) != 0)
{
perror("Cannot destroy the attribute object
");
return -1; /* or exit*/
}
/* ***************************************************** */
/* Example for pthread_attr_getdetachstate */
/* ***************************************************** */
pthread_attr_t new_attr;
int detach_state;
/* Initialize attribute */
if(pthread_attr_init(&new;_attr) != 0)
{
printf("Cannot initialize attribute object
");
return -1;
}
if(pthread_attr_getdetachstate(&new;_attr, &detach;_state) != 0)
{
printf("Cannot get the state
");
return -1;
}
if(detach_state == PTHREAD_CREATE_JOINABLE)
{
printf("State is joinable
");
}
else
{
printf("State is detached
");
}
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_getschedpolicy */
/* ***************************************************** */
pthread_attr_t new_attr;
int rc;
int policy;
/* Initialize attribute */
if(pthread_attr_init(&new;_attr) != 0)
{
printf("Cannot initialize attribute object
");
return -1; /* or exit */
}
rc = pthread_attr_getschedpolicy(new_attr, &policy;);
if( rc != 0) {
printf("pthread_attr_getschedpolicy failed
");
return -1;
}
switch(policy) {
case SCHED_FIFO:
printf("Scheduling policy: SCHED_FIFO 0);
break;
case SCHED_RR:
printf("Scheduling policy: SCHED_RR
");
break;
case SCHED_OTHER:
printf("Scheduling policy: SCHED_OTHER
");
break;
}
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_getscope */
/* ***************************************************** */
pthread_attr_t new_attr;
int rc;
int scope;
/* Initialize attribute */
if(pthread_attr_init(&new;_attr) != 0)
{
printf("Cannot initialize attribute object
");
return -1; /* or exit */
}
rc = pthread_attr_getscope(new_attr, &scope;);
if( rc != 0) {
printf("pthread_attr_getscope failed");
return -1;
}
switch(scope) {
case SYSTEMSCOPE:
printf("scope: System
");
break;
case PROCESSSCOPE:
printf("scope: Process
");
break;
}
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_getstacksize */
/* ***************************************************** */
pthread_attr_t new_attr;
size_t ssize;
int rc;
/* Initialize attribute */
if(pthread_attr_init(&new;_attr) != 0)
{
printf("Cannot initialize attribute object
");
return -1; /* or exit */
}
/* Get the default stack_size value */
rc = pthread_attr_getstacksize(&new;_attr, &stack;_size);
if( rc != 0) {
printf("pthread_attr_getstacksize failed");
return -1;
}
printf("ssize = %lu
", stack_size);
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_getschedparam */
/* ***************************************************** */
pthread_attr_t attr;
int rc=0;
struct sched_param param;
struct sched_param param2;
rc = pthread_attr_init(&attr;);
if(rc != 0) {
printf("pthread_attr_init failed
");
return -1;
}
param.sched_priority = 50;
rc = pthread_attr_setschedparam(&attr;, & param);
if(rc != 0) {
printf("pthread_attr_setschedparam failed
");
return -1;
}
rc = pthread_attr_getschedparam(&attr;, & param2);
if(rc != 0) {
printf("pthread_attr_getschedparam failed
");
return -1;
}
/* priority will be stored in param2.sched_priority */
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_setdetachstate */
/* ***************************************************** */
pthread_attr_t new_attr;
int detach_state;
/* Initialize attribute */
if(pthread_attr_init(&new;_attr) != 0)
{
perror("Cannot initialize attribute object
");
return -1;
}
/* Set the attribute object to PTHREAD_CREATE_JOINABLE. */
if(pthread_attr_setdetachstate(&new;_attr, PTHREAD_CREATE_JOINABLE) != 0)
{
perror("Error in pthread_attr_setdetachstate()
");
return -1;
}
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_setschedparam */
/* ***************************************************** */
pthread_attr_t attr;
int rc=0;
struct sched_param param;
rc = pthread_attr_init(&attr;);
if(rc != 0) {
printf("pthread_attr_init failed
");
return -1;
}
param.sched_priority = 50;
rc = pthread_attr_setschedparam(&attr;, & param);
if(rc != 0) {
printf("pthread_attr_setschedparam failed
");
return -1;
}
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_setschedpolicy */
/* ***************************************************** */
pthread_attr_t attr;
int rc;
int policy = SCHED_RR;
if(pthread_attr_init(&attr;) != 0) {
printf("Error on pthread_attr_init()
");
return -1;
}
if((rc=pthread_attr_setschedpolicy(&attr;,policy)) != 0) {
printf("Error on pthread_attr_setschedpolicy() rc=%d
", rc);
return -1;
}
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_setstacksize */
/* ***************************************************** */
pthread_attr_t attr;
int rc;
int policy = SCHED_RR;
if(pthread_attr_init(&attr;) != 0) {
printf("Error on pthread_attr_init()
");
return -1;
}
if((rc=pthread_attr_setstacksize(&attr;,8192)) != 0) {
printf("Error on pthread_attr_setstacksize() rc=%d
", rc);
return -1;
}
/* Create thread */
/* Destroy attribute */
/* ***************************************************** */
/* Example for pthread_attr_setscope */
/* ***************************************************** */
pthread_attr_t attr;
int rc;
/* Initialize attr */
rc = pthread_attr_init(&attr;);
if( rc != 0) {
perror("pthread_attr_init failed");
return -1;
}
rc = pthread_attr_setscope(&attr;, PTHREAD_SCOPE_SYSTEM);
if (rc != 0 ) {
perror("PTHREAD_SCOPE_SYSTEM is not supported");
return -1;
}
/* Create thread */
/* Destroy attribute */
Parameters
pthread_attr_t *attrib |
Note: This description also covers the following functions - pthread_attr_destroy(pthread_attr_t *)pthread_attr_destroy(pthread_attr_t *) pthread_attr_setstacksize(pthread_attr_t *,size_t)pthread_attr_setstacksize(pthread_attr_t *,size_t) pthread_attr_getstacksize(const pthread_attr_t *,size_t *)pthread_attr_getstacksize(const pthread_attr_t *,size_t *) pthread_attr_setdetachstate(pthread_attr_t *,int)pthread_attr_setdetachstate(pthread_attr_t *,int) pthread_attr_getdetachstate(const pthread_attr_t *,int *)pthread_attr_getdetachstate(const pthread_attr_t *,int *) pthread_attr_setschedparam(pthread_attr_t *,const struct sched_param *)pthread_attr_setschedparam(pthread_attr_t *,const struct sched_param *) pthread_attr_getschedparam(const pthread_attr_t *,struct sched_param *)pthread_attr_getschedparam(const pthread_attr_t *,struct sched_param *) pthread_attr_setschedpolicy(pthread_attr_t *,int)pthread_attr_setschedpolicy(pthread_attr_t *,int) pthread_attr_getschedpolicy(const pthread_attr_t *,int *)pthread_attr_getschedpolicy(const pthread_attr_t *,int *) pthread_attr_setscope(pthread_attr_t *,int)pthread_attr_setscope(pthread_attr_t *,int) pthread_attr_getscope(const pthread_attr_t *,int *)pthread_attr_getscope(const pthread_attr_t *,int *)
|
|
Return value
int |
If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.
|
|
See also:
pthread_attr_destroy(pthread_attr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_destroy(pthread_attr_t *attrib);
Description
Parameters
Return value
See also:
pthread_attr_getdetachstate(const pthread_attr_t *,int *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_getdetachstate(const pthread_attr_t *attrib, int *detState);
Description
Parameters
Return value
See also:
pthread_attr_setdetachstate(pthread_attr_t *,int)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_setdetachstate(pthread_attr_t *attrib, int detState);
Description
Parameters
Return value
See also:
pthread_attr_getstacksize(const pthread_attr_t *,size_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_getstacksize(const pthread_attr_t *attrib, size_t *stkSize);
Description
Parameters
Return value
See also:
pthread_attr_setstacksize(pthread_attr_t *,size_t)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_setstacksize(pthread_attr_t *attrib, size_t stkSize);
Description
Parameters
Return value
See also:
pthread_once(pthread_once_t *,thread_init_routine)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_once(pthread_once_t *once_control, thread_init_routine init_routine);
Description
The first call to pthread_once by any thread in a process, with a given once_control , will call the init_routine with no arguments. Subsequent calls to
pthread_once with the same once_control will not call the init_routine . On return from pthread_once , it is guaranteed that init_routine has completed. The once_control parameter is used to determine whether the associated
initialization routine has been called.
The constant PTHREAD_ONCE_INIT is defined by header #include <pthread.h> .
The behavior of pthread_once is undefined if once_control has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT .
Examples:
/* The init function that pthread_once calls */
void an_init_func_1_1()
{
printf (" Initialized ..
");
}
int XYZ()
{
pthread_once_t once_control = PTHREAD_ONCE_INIT;
/* Call pthread_once, passing it the once_control */
pthread_once(&once;_control, an_init_func_1_1);
/* Call pthread_once again. The init function should not be
called. */
pthread_once(&once;_control, an_init_func_1_1);
}
Parameters
Return value
int |
If successful, the pthread_once function will return zero. Otherwise an error number will be returned to indicate the error.
|
|
pthread_mutexattr_init(pthread_mutexattr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutexattr_init(pthread_mutexattr_t *attr);
Description
Mutex attributes are used to specify parameters to pthread_mutex_init . One attribute object can be used in multiple calls
to pthread_mutex_init , with or without modifications between calls.
The pthread_mutexattr_init function initializes attr with all the default mutex attributes.
The pthread_mutexattr_destroy function destroys attr .
The pthread_mutexattr_set* functions set the attribute that corresponds to each function name.
The pthread_mutexattr_get* functions copy the value of the attribute that corresponds to each function name to the location
pointed to by the second function parameter.
Examples:
pthread_mutexattr_t mta;
int rc;
/* Initialize a mutex attributes object */
if((rc=pthread_mutexattr_init(&mta;)) != 0)
{
fprintf(stderr,"Cannot initialize mutex attributes object
");
return -1;
}
/* Destroy the mutex attributes object */
if(pthread_mutexattr_destroy(&mta;) != 0)
{
fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
", rc);
return -1;
}
pthread_mutexattr_t mta;
int pshared;
/* Initialize a mutex attributes object */
if(pthread_mutexattr_init(&mta;) != 0)
{
perror("Error at pthread_mutexattr_init()
");
return -1;
}
/* The default 'pshared' attribute is PTHREAD_PROCESS_PRIVATE */
if(pthread_mutexattr_getpshared(&mta;, &pshared;) != 0)
{
fprintf(stderr,"Error obtaining the attribute process-shared
");
return -1;
}
if (pshared != PTHREAD_PROCESS_PRIVATE)
printf (" pshared is NOT PTHREAD_PROCESS_PRIVATE
");
pthread_mutexattr_t mta;
int type;
/* Initialize a mutex attributes object */
if(pthread_mutexattr_init(&mta;) != 0)
{
perror("Error at pthread_mutexattr_init()
");
return -1;
}
/* The default 'type' attribute is PTHREAD_MUTEX_DEFAULT */
if(pthread_mutexattr_gettype(&mta;, &type;) != 0)
{
fprintf(stderr,"pthread_mutexattr_gettype(): Error obtaining the attribute 'type'
");
return -1;
}
if(type != PTHREAD_MUTEX_DEFAULT)
{
printf("FAILED: Incorrect default mutexattr 'type' value: %d
", type);
return -1;
}
pthread_mutexattr_t mta;
int rc;
/* Initialize a mutex attributes object */
if((rc=pthread_mutexattr_init(&mta;)) != 0)
{
fprintf(stderr,"Cannot initialize mutex attributes object
");
return -1;
}
/* Destroy the mutex attributes object */
if(pthread_mutexattr_destroy(&mta;) != 0)
{
fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
", rc);
return -1;
}
pthread_mutexattr_t mta;
int ret;
/* Initialize a mutex attributes object */
if(pthread_mutexattr_init(&mta;) != 0)
{
perror("Error at pthread_mutexattr_init()
");
return -1;
}
/* Set the 'pshared' attribute to PTHREAD_PROCESS_PRIVATE */
if((ret=pthread_mutexattr_setpshared(&mta;, PTHREAD_PROCESS_PRIVATE)) != 0)
{
printf("FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_PRIVATE. Error: %d
", ret);
return -1;
}
pthread_mutexattr_t mta;
int type;
/* Initialize a mutex attributes object */
if(pthread_mutexattr_init(&mta;) != 0)
{
perror("Error at pthread_mutexattr_init()
");
return -1;
}
if(pthread_mutexattr_gettype(&mta;, &type;) != 0)
{
printf("Error getting the attribute 'type'
");
return -1;
}
if(type != PTHREAD_MUTEX_DEFAULT)
{
printf("FAILED: Default value of the 'type' attribute is not PTHREAD_MUTEX_DEFAULT
");
return -1;
}
if(pthread_mutexattr_settype(&mta;, PTHREAD_MUTEX_NORMAL) != 0)
{
printf("FAILED: Error setting the attribute 'type'
");
return -1;
}
Parameters
Return value
int |
If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.
|
|
pthread_mutexattr_destroy(pthread_mutexattr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
Description
Parameters
Return value
pthread_mutexattr_getpshared(const pthread_mutexattr_t *,int *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared);
Description
Parameters
Return value
pthread_mutexattr_gettype(pthread_mutexattr_t *,int *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type);
Description
Parameters
Return value
pthread_mutexattr_settype(pthread_mutexattr_t *,int)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
Description
Parameters
Return value
pthread_mutex_init(pthread_mutex_t *,const pthread_mutexattr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
Description
The pthread_mutex_init function creates a new mutex, with attributes specified with attr . If attr is NULL the default attributes
are used.
Examples:
pthread_mutex_t mutex;
int XYZ()
{
int rc;
/* Initialize a mutex object with the default mutex attributes */
if((rc=pthread_mutex_init(&mutex;,NULL)) != 0)
{
fprintf(stderr,"Error at pthread_mutex_init(), rc=%d0,rc);
return -1;
}
}
Parameters
Return value
int |
If successful, pthread_mutex_init will return zero and put the new mutex id into mutex , otherwise an error number will be
returned to indicate the error.
|
|
pthread_mutex_destroy(pthread_mutex_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutex_destroy(pthread_mutex_t *mutex);
Description
The pthread_mutex_destroy function frees the resources allocated for mutex .
Examples:
pthread_mutexattr_t mta;
int rc;
/* Initialize a mutex attributes object */
if((rc=pthread_mutexattr_init(&mta;)) != 0) {
fprintf(stderr,"Error at pthread_mutexattr_init(), rc=%d
",rc);
return -1;
}
/* Destroy the mutex attributes object */
if((rc=pthread_mutexattr_destroy(&mta;)) != 0) {
fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
",rc);
return -1;
}
Parameters
Return value
int |
If successful, pthread_mutex_destroy will return zero, otherwise an error number will be returned to indicate the error.
|
|
pthread_mutex_lock(pthread_mutex_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutex_lock(pthread_mutex_t *mutex);
Description
The pthread_mutex_lock function locks mutex . If the mutex is already locked, the calling thread will block until the mutex
becomes available.
Examples:
pthread_mutex_t mutex;
int XYZ()
{
int rc;
/* Initialize a mutex object with the default mutex attributes */
if((rc=pthread_mutex_init(&mutex;,NULL)) != 0) {
fprintf(stderr,"Error at pthread_mutex_init(), rc=%d
",rc);
return -1;
}
/* Lock the mutex using pthread_mutex_lock() */
if((rc=pthread_mutex_lock(&mutex;)) == 0) {
printf(" lock is successful
");
}
}
Parameters
Return value
int |
If successful, pthread_mutex_lock will return zero, otherwise an error number will be returned to indicate the error.
|
|
pthread_mutex_timedlock(pthread_mutex_t *,const struct timespec *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
Description
The pthread_mutex_timedlock function will lock mutex . If it is already locked the calling thread will block until the mutex
becomes available or the timeout, specified by abstime, expires. The time of the timeout is an absolute time and is not relative
to the current time.
Examples:
#include <sys/time.h>
#define TIMEOUT 3
/* 3 seconds of timeout time for pthread_mutex_timedlock(). */
struct timespec timeout;
struct timeval currsec1;
int rc;
pthread_mutex_t mutex;
/* Get the current time before the mutex locked. */
gettimeofday(&currsec1;, NULL);
/* Absolute time, not relative. */
timeout.tv_sec = currsec1.tv_sec + TIMEOUT;
timeout.tv_nsec = currsec1.tv_usec * 1000;
printf("Timed mutex lock will block for %d seconds starting from: %ld.%06ld
", TIMEOUT, (long)currsec1.tv_sec, (long)currsec1.tv_usec);
rc = pthread_mutex_timedlock(&mutex;, &timeout;);
if(rc != 0) {
if (rc == ETIMEDOUT) {
fprintf(stderr,"Thread stops waiting when time is out
");
}
else {
fprintf(stderr,"pthread_mutex_timedwait return %d
", rc);
}
}
fprintf(stderr,"Thread wakened up
");
Parameters
Return value
int |
If successful, pthread_mutex_timedlock will return zero, otherwise an error number will be returned to indicate the error.
|
|
pthread_mutex_trylock(pthread_mutex_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutex_trylock(pthread_mutex_t *mutex);
Description
The pthread_mutex_trylock function locks mutex . If the mutex is already locked, pthread_mutex_trylock will not block waiting
for the mutex, but will return an error condition.
Examples:
int rc;
pthread_mutex_t mutex;
rc = pthread_mutex_trylock(&mutex;);
switch (rc)
{
case 0:
printf ("Locked successfully
");
break;
case EAGAIN:
printf ("could not lock, try later.....
");
break;
}
Parameters
Return value
int |
If successful, pthread_mutex_trylock will return zero, otherwise an error number will be returned to indicate the error.
|
|
pthread_mutex_unlock(pthread_mutex_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_mutex_unlock(pthread_mutex_t *mutex);
Description
If the current thread holds the lock on mutex , then the pthread_mutex_unlock function unlocks mutex .
Examples:
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int XYZ()
{
int rc;
/* Get the mutex using pthread_mutex_lock() */
if((rc=pthread_mutex_lock(&mutex;)) != 0) {
fprintf(stderr,"Error at pthread_mutex_lock(), rc=%d
",rc);
return -1;
}
/* Release the mutex using pthread_mutex_unlock() */
if((rc=pthread_mutex_unlock(&mutex;)) != 0) {
printf("unlock failed
");
return -1;
}
Parameters
Return value
int |
If successful, pthread_mutex_unlock will return zero, otherwise an error number will be returned to indicate the error.
|
|
pthread_condattr_init(pthread_condattr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_condattr_init(pthread_condattr_t *);
Description
Note: This description also covers the following functions - pthread_condattr_destroy(pthread_condattr_t *)pthread_condattr_destroy(pthread_condattr_t *)
Condition attribute objects are used to specify parameters to pthread_cond_init . Current implementation of conditional variables
does not support any attributes, so these functions are not very useful.
The pthread_condattr_init function initializes a condition attribute object with the default attributes.(As of now None)
The pthread_condattr_destroy function destroys a condition attribute object.
Examples:
pthread_condattr_t condattr;
int rc;
/* Initialize a condition variable attributes object */
if((rc=pthread_condattr_init(&condattr;)) != 0)
{
fprintf(stderr,"Cannot initialize condition variable attributes object
");
return -1;
}
/* Destroy the condition variable attributes object */
if(pthread_condattr_destroy(&condattr;) != 0)
{
fprintf(stderr,"Error at pthread_condattr_destroy(), rc=%d
", rc);
return -1;
}
Parameters
Return value
int |
If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.
|
|
pthread_condattr_destroy(pthread_condattr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_condattr_destroy(pthread_condattr_t *attr);
Description
Refer pthread_condattr_init(pthread_condattr_t *)pthread_condattr_init(pthread_condattr_t *) for the documentation
Parameters
Return value
pthread_cond_init(pthread_cond_t *,const pthread_condattr_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *);
Description
The pthread_cond_init function creates a new condition variable referenced by cond with attributes referenced by attr. If
attr is NULL, the default condition variable attributes will be used. Upon successful initialization, the state of the condition
variable will be initialized.
Examples:
int rc;
struct testdata
{
pthread_mutex_t mutex;
pthread_cond_t cond;
};
static struct testdata td;
int function1()
{
/* Initialize a condition variable object */
if (pthread_cond_init(&td.cond;, NULL) != 0) {
fprintf(stderr,"Fail to initialize cond
");
return -1;
}
/* ... Use it for wait, signal, broadcast */
/* Destroy the condition variable object */
if((rc=pthread_cond_destroy(&td.cond;)) != 0)
{
fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
",rc);
return -1;
}
}
Parameters
Return value
int |
If successful, the pthread_cond_init function will return zero and put the new condition variable id into cond , otherwise
an error number will be returned to indicate the error.
|
|
pthread_cond_destroy(pthread_cond_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_cond_destroy(pthread_cond_t *cond);
Description
The pthread_cond_destroy function frees the resources allocated by the condition variable cond .
Examples:
int rc;
struct testdata
{
pthread_mutex_t mutex;
pthread_cond_t cond;
};
static struct testdata td;
int function1()
{
/* Initialize a condition variable object */
if (pthread_cond_init(&td.cond;, NULL) != 0) {
fprintf(stderr,"Fail to initialize cond
");
return -1;
}
/* ... Use it for wait, signal, broadcast */
/* Destroy the condition variable object */
if((rc=pthread_cond_destroy(&td.cond;)) != 0)
{
fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
",rc);
return -1;
}
}
Parameters
Return value
int |
If successful, the pthread_cond_destroy function will return zero, otherwise an error number will be returned to indicate
the error.
|
|
pthread_cond_wait(pthread_cond_t *,pthread_mutex_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
Description
The pthread_cond_wait function atomically blocks the current thread waiting on the condition variable specified by cond ,
and unblocks the mutex specified by mutex . The waiting thread unblocks only after another thread calls pthread_cond_signal
, or pthread_cond_broadcast with the same condition variable, and the current thread reacquires the lock on mutex .
Examples:
struct testdata
{
pthread_mutex_t mutex;
pthread_cond_t cond;
};
static struct testdata td;
int rc;
/* mutex and conditional variable must be initialized */
if (pthread_mutex_lock(&td.mutex;) != 0) {
fprintf(stderr,"Thread failed to acquire mutex
");
return -1;
}
fprintf(stderr,"Thread started
");
fprintf(stderr,"Thread is waiting for the cond
");
rc = pthread_cond_wait(&td.cond;, &td.mutex;);
if (rc != 0) {
fprintf(stderr,"pthread_cond_wait return %d
", rc);
return -1;
}
fprintf(stderr,"Thread wakened
");
pthread_mutex_unlock(&td.mutex;);
Parameters
Return value
int |
If successful, the pthread_cond_wait function will return zero. Otherwise an error number will be returned to indicate the
error.
|
|
pthread_cond_signal(pthread_cond_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_cond_signal(pthread_cond_t *cond);
Description
The pthread_cond_signal function unblocks one thread waiting for the condition variable cond .
Examples:
int rc;
struct testdata
{
pthread_mutex_t mutex;
pthread_cond_t cond;
};
static struct testdata td;
int function1()
{
if (pthread_mutex_init(&td.mutex;, NULL) != 0) {
fprintf(stderr,"Fail to initialize mutex
");
return -1;
}
if (pthread_cond_init(&td.cond;, NULL) != 0) {
fprintf(stderr,"Fail to initialize cond
");
return -1;
}
/* ....... other thread wait for signal */
/* Acquire the mutex to make sure that all waiters are currently
blocked on pthread_cond_wait */
if (pthread_mutex_lock(&td.mutex;) != 0) {
fprintf(stderr,"Main: Fail to acquire mutex
");
return -1;
}
if (pthread_mutex_unlock(&td.mutex;) != 0) {
fprintf(stderr,"Main: Fail to release mutex
");
return -1;
}
/* signal the condition to wake up all waiters */
fprintf(stderr," signal the condition
");
rc = pthread_cond_signal(&td.cond;);
if (rc != 0) {
fprintf(stderr," failed to signal the condition
");
return -1;
}
}
Parameters
Return value
int |
If successful, the pthread_cond_signal function will return zero, otherwise an error number will be returned to indicate the
error.
|
|
pthread_cond_broadcast(pthread_cond_t *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_cond_broadcast(pthread_cond_t *cond);
Description
The pthread_cond_broadcast function unblocks all threads waiting for the condition variable cond .
Examples:
int rc;
struct testdata
{
pthread_mutex_t mutex;
pthread_cond_t cond;
};
static struct testdata td;
if (pthread_mutex_init(&td.mutex;, NULL) != 0) {
fprintf(stderr,"Fail to initialize mutex
");
return -1;
}
if (pthread_cond_init(&td.cond;, NULL) != 0) {
fprintf(stderr,"Fail to initialize cond
");
return -1;
}
/* ....... other thread wait for signal */
/* Acquire the mutex to make sure that all waiters are currently
blocked on pthread_cond_wait */
if (pthread_mutex_lock(&td.mutex;) != 0) {
fprintf(stderr,"Main: Fail to acquire mutex
");
return -1;
}
if (pthread_mutex_unlock(&td.mutex;) != 0) {
fprintf(stderr,"Main: Fail to release mutex
");
return -1;
}
/* signal the condition to wake up all waiters */
fprintf(stderr," signal the condition0);
rc = pthread_cond_broadcast(&td.cond;);
if (rc != 0) {
fprintf(stderr," failed to signal the condition
");
return -1;
}
Parameters
Return value
int |
If successful, the pthread_cond_broadcast function will return zero, otherwise an error number will be returned to indicate
the error.
|
|
pthread_key_create(pthread_key_t *,destructor_routine)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_key_create(pthread_key_t *key, destructor_routine dest);
Description
The pthread_key_create function creates a thread-specific data key visible to all threads in the process. Key values provided
by pthread_key_create are opaque objects used to locate thread-specific data. Although the same key value may be used by different
threads, the values bound to the key by pthread_setspecific are maintained on a per-thread basis and persist for the life
of the calling thread.
Upon key creation, the value NULL is associated with the new key in all active threads. Upon thread creation, the value NULL
is associated with all defined keys in the new thread.
An optional destructor function dest may be associated with each key value. At thread exit, if a key value has a non-NULL
destructor pointer, and the thread has a non-NULL value associated with the key, the function pointed to is called with the
current associated value as its sole argument. The order of destructor calls is unspecified if more than one destructor exists
for a thread when it exits.
If, after all the destructors have been called for all non-NULL values with associated destructors, there are still some non-NULL
values with associated destructors, then the process is repeated. If, after at least [PTHREAD_DESTRUCTOR_ITERATIONS] iterations
of destructor calls for outstanding non-NULL values, there are still some non-NULL values with associated destructors, the
implementation stops calling destructors.
Examples:
pthread_key_t keys;
if(pthread_key_create(&keys;, NULL) != 0)
{
printf("Error: pthread_key_create() failed
");
return -1;
}
Parameters
pthread_key_t *key |
|
destructor_routine dest |
|
|
Return value
int |
If successful, the pthread_key_create function will store the newly created key value at the location specified by key and
returns zero. Otherwise an error number will be returned to indicate the error.
|
|
pthread_key_delete(pthread_key_t)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_key_delete(pthread_key_t key);
Description
The pthread_key_delete function deletes a thread-specific data key previously returned by pthread_key_create . The thread-specific
data values associated with key need not be NULL at the time that pthread_key_delete is called. It is the responsibility of
the application to free any application storage or perform any cleanup actions for data structures related to the deleted
key or associated thread-specific data in any threads; this cleanup can be done either before or after pthread_key_delete
is called. Any attempt to use key following the call to pthread_key_delete results in undefined behavior.
The pthread_key_delete function is callable from within destructor functions. Destructor functions are not invoked by pthread_key_delete
. Any destructor function that may have been associated with key will no longer be called upon thread exit.
Examples:
pthread_key_t keys;
if(pthread_key_create(&keys;, NULL) != 0)
{
printf("Error: pthread_key_create() failed
");
return -1;
}
if(pthread_key_delete(keys) != 0)
{
printf("Error: pthread_key_delete() failed
");
return -1;
}
Parameters
Return value
int |
If successful, the pthread_key_delete function will return zero. Otherwise an error number will be returned to indicate the
error.
|
|
pthread_setspecific(pthread_key_t,const void *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_setspecific(pthread_key_t key, const void *val);
Description
The pthread_setspecific function associates a thread-specific value with a key obtained via a previous call to pthread_key_create
. Different threads can bind different values to the same key. These values are typically pointers to blocks of dynamically
allocated memory that have been reserved for use by the calling thread.
The effect of calling pthread_setspecific with a key value not obtained from pthread_key_create or after key has been deleted
with pthread_key_delete is undefined.
The pthread_setspecific function may be called from a thread-specific data destructor function, however this may result in
lost storage or infinite loops.
Examples:
pthread_key_t keys;
void* rc;
if(pthread_key_create(&keys;, NULL) != 0)
{
printf("Error: pthread_key_create() failed
");
return -1;
} else
{
if(pthread_setspecific(keys, (void *)(long)(100)) != 0)
{
printf("Error: pthread_setspecific() failed
");
return -1;
}
}
Parameters
pthread_key_t key |
|
const void *val |
|
|
Return value
int |
If successful, the pthread_setspecific function will return zero. Otherwise an error number will be returned to indicate the
error.
|
|
pthread_getspecific(pthread_key_t)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C void* pthread_getspecific(pthread_key_t key);
Description
The pthread_getspecific function returns the value currently bound to the specified key on behalf of the calling thread.
The effect of calling pthread_getspecific with a key value not obtained from pthread_key_create or after key has been deleted
with pthread_key_delete is undefined.
The pthread_getspecific function may be called from a thread-specific data destructor function.
Examples:
pthread_key_t keys;
void* rc;
if(pthread_key_create(&keys;, NULL) != 0)
{
printf("Error: pthread_key_create() failed0);
return -1;
} else
{
if(pthread_setspecific(keys, (void *)(long)(100)) != 0)
{
printf("Error: pthread_setspecific() failed
");
return -1;
}
}
rc = pthread_getspecific(keys);
if(rc != (void *)(long)(100))
{
printf("getspecific failed
");
return -1;
}
Parameters
pthread_attr_getscope(const pthread_attr_t *,int *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_getscope(const pthread_attr_t *attrib, int *scope);
Description
Parameters
Return value
See also:
pthread_attr_setscope(pthread_attr_t *,int)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_setscope(pthread_attr_t *attrib, int conscope);
Description
Parameters
Return value
See also:
pthread_attr_setschedpolicy(pthread_attr_t *,int)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_setschedpolicy(pthread_attr_t *attrib, int policy);
Description
Parameters
Return value
See also:
pthread_attr_getschedpolicy(const pthread_attr_t *,int *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_getschedpolicy(const pthread_attr_t *attrib, int *policy);
Description
Parameters
Return value
See also:
pthread_attr_getschedparam(const pthread_attr_t *,struct sched_param *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_getschedparam(const pthread_attr_t *attrib, struct sched_param *param);
Description
Parameters
Return value
See also:
pthread_attr_setschedparam(pthread_attr_t *,const struct sched_param *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_attr_setschedparam(pthread_attr_t *attrib, const struct sched_param *param);
Description
Parameters
Return value
See also:
pthread_getschedparam(pthread_t,int *,struct sched_param *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_getschedparam(pthread_t thr, int *policy, struct sched_param *param);
Description
Parameters
Return value
pthread_setschedparam(pthread_t,int,const struct sched_param *)
| Interface status: |
externallyDefinedApi |
|
IMPORT_C int pthread_setschedparam(pthread_t th, int policy, const struct sched_param *param);
Description
The pthread_setschedparam and pthread_getschedparam functions set and get the scheduling parameters of individual threads.
The scheduling policy for a thread will be SCHED_RR (round-robin). ( SCHED_FIFO is not supported) The thread priority (accessed
via param->sched_priority ) must be at least PTHREAD_MIN_PRIORITY and no more than PTHREAD_MAX_PRIORITY(as returned by sched_get_priority_max(int)sched_get_priority_max(int) and sched_get_priority_min(int)sched_get_priority_min(int) APIs) .
Examples:
struct sched_param sparam;
int policy, priority, policy_1;
int rc;
policy = SCHED_RR;
priority = 50;
sparam.sched_priority = priority;
rc = pthread_setschedparam(pthread_self(), policy, &sparam;);
if (rc != 0)
{
printf("Error at pthread_setschedparam: rc=%d
", rc);
return -1;
}
int e ;
struct sched_param param;
pthread_t thread; // Thread which will be assigned the scheduling priority and the policy.
pthread_t thread = pthread_self();
param.sched_priority = 100;
e = pthread_setschedparam(thread,SCHED_RR, & param);
if(e != 0)
{
printf("setting scheduling policy and priority failed."));
return -1;
}
int e ;
struct sched_param param;
pthread_t thread; // Thread which will be assigned the scheduling priority and the policy.
int policy;
pthread_t thread = pthread_self();
param.sched_priority = 100;
e = pthread_setschedparam(thread,SCHED_RR, & param);
if(e != 0)
{
printf("setting scheduling policy and priority failed."));
return -1;
}
e = pthread_getschedparam(thread,&policy;,& param);
if (e != 0)
{
printf("getting scheduling policy and priority failed."));
return -1;
}
Parameters
Return value
int |
If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.
|
|