Enumerator | Value | Description |
---|---|---|
_ENeedsNormalInit | -3 | |
_ENeedsRecursiveInit | ||
_ENeedsErrorCheckInit | ||
_EInitialized | 1 | |
_EDestroyed | ||
_EInvalid |
Enumerator | Value | Description |
---|---|---|
_ELockNotCreated | ||
_ELockCreating | ||
_ELockCreated |
typedef struct _pthread_condattr_t * | pthread_condattr_t |
Used to identify a condition attribute object
Enumerator | Value | Description |
---|---|---|
PTHREAD_CREATE_JOINABLE | 0 | |
PTHREAD_CREATE_DETACHED | 1 |
Enumerator | Value | Description |
---|---|---|
PTHREAD_SCOPE_SYSTEM | 1 |
Enumerator | Value | Description |
---|---|---|
PTHREAD_CANCEL_ASYNCHRONOUS | 0 | |
PTHREAD_CANCEL_DEFERRED | 1 |
Enumerator | Value | Description |
---|---|---|
PTHREAD_PROCESS_PRIVATE | 0 |
Enumerator | Value | Description |
---|---|---|
PTHREAD_MUTEX_NORMAL | ||
PTHREAD_MUTEX_RECURSIVE | ||
PTHREAD_MUTEX_ERRORCHECK | ||
PTHREAD_MUTEX_DEFAULT | PTHREAD_MUTEX_NORMAL |
Enumerator | Value | Description |
---|---|---|
PTHREAD_EXPLICIT_SCHED | 0 | |
PTHREAD_INHERIT_SCHED | 1 |
typedef void *(* | thread_begin_routine |
IMPORT_C int | pthread_create | ( | pthread_t * | threadhdl, |
pthread_attr_t * | attrib, | |||
thread_begin_routine | begin_routine, | |||
void * | param | |||
) |
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.
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; } }
See also: pthread_exit() pthread_join()
IMPORT_C pthread_t | pthread_self | ( | ) |
The pthread_self function returns the thread ID of the calling thread.
pthread_t new_th2; new_th2=pthread_self();
See also: pthread_create() pthread_equal()
The pthread_equal function compares the thread IDs t1 and t2 .
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; } }
See also: pthread_create() pthread_exit()
IMPORT_C int | pthread_join | ( | pthread_t | thHandle, |
void ** | retValPtr | |||
) |
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.
/* 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; }
See also: wait() pthread_create()
IMPORT_C int | pthread_detach | ( | pthread_t | thrHandle | ) |
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.
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; }
See also: pthread_join()
IMPORT_C void | pthread_exit | ( | void * | retValPtr | ) |
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.
/* Thread's function. */ void *a_thread_func_1_1(void*) { /* .. Do some thing .. */ pthread_exit((void*)RETURN_CODE); }
See also: _exit() exit() pthread_create() pthread_join()
IMPORT_C int | pthread_attr_init | ( | pthread_attr_t * | attrib | ) |
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.
/* ***************************************************** */ /* 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 */
See also: pthread_create()
Parameters | |
---|---|
attrib | Note: This description also covers the following functions - pthread_attr_destroy() pthread_attr_setstacksize() pthread_attr_getstacksize() pthread_attr_setdetachstate() pthread_attr_getdetachstate() pthread_attr_setschedparam() pthread_attr_getschedparam() pthread_attr_setschedpolicy() pthread_attr_getschedpolicy() pthread_attr_setscope() pthread_attr_getscope() |
IMPORT_C int | pthread_attr_destroy | ( | pthread_attr_t * | attrib | ) |
See also: pthread_create()
Parameters | |
---|---|
attrib | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_getdetachstate | ( | const pthread_attr_t * | attrib, |
int * | detState | |||
) |
See also: pthread_create()
Parameters | |
---|---|
detState | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_setdetachstate | ( | pthread_attr_t * | attrib, |
int | detState | |||
) |
See also: pthread_create()
Parameters | |
---|---|
detState | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_getstacksize | ( | const pthread_attr_t * | attrib, |
size_t * | stkSize | |||
) |
See also: pthread_create()
Parameters | |
---|---|
stkSize | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_setstacksize | ( | pthread_attr_t * | attrib, |
size_t | stkSize | |||
) |
See also: pthread_create()
Parameters | |
---|---|
stkSize | Refer pthread_attr_init() for the documentation |
typedef void(* | thread_init_routine |
IMPORT_C int | pthread_once | ( | pthread_once_t * | once_control, |
thread_init_routine | init_routine | |||
) |
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 .
/* 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); }
IMPORT_C int | pthread_mutexattr_init | ( | pthread_mutexattr_t * | attr | ) |
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.
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; }
See also: pthread_mutex_init()
Parameters | |
---|---|
attr | Note: This description also covers the following functions - pthread_mutexattr_destroy() pthread_mutexattr_settype() pthread_mutexattr_gettype() pthread_mutexattr_getpshared() pthread_mutexattr_setpshared() |
IMPORT_C int | pthread_mutexattr_destroy | ( | pthread_mutexattr_t * | attr | ) |
See also: pthread_mutex_init()
Parameters | |
---|---|
attr | Refer pthread_mutexattr_init() for the documentation |
IMPORT_C int | pthread_mutexattr_getpshared | ( | const pthread_mutexattr_t * | attr, |
int * | pshared | |||
) |
See also: pthread_mutex_init()
Parameters | |
---|---|
pshared | Refer pthread_mutexattr_init() for the documentation |
IMPORT_C int | pthread_mutexattr_setpshared | ( | pthread_mutexattr_t * | attr, |
int | pshared | |||
) |
IMPORT_C int | pthread_mutexattr_gettype | ( | pthread_mutexattr_t * | attr, |
int * | type | |||
) |
See also: pthread_mutex_init()
Parameters | |
---|---|
type | Refer pthread_mutexattr_init() for the documentation |
IMPORT_C int | pthread_mutexattr_settype | ( | pthread_mutexattr_t * | attr, |
int | type | |||
) |
See also: pthread_mutex_init()
Parameters | |
---|---|
type | # use integer valuses between 1 and 10 Refer pthread_mutexattr_init() for the documentation |
IMPORT_C int | pthread_mutex_init | ( | pthread_mutex_t * | mutex, |
const pthread_mutexattr_t * | attr | |||
) |
The pthread_mutex_init function creates a new mutex, with attributes specified with attr . If attr is NULL the default attributes are used.
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; } }
See also: pthread_mutex_destroy() pthread_mutex_lock() pthread_mutex_trylock() pthread_mutex_unlock()
IMPORT_C int | pthread_mutex_destroy | ( | pthread_mutex_t * | mutex | ) |
The pthread_mutex_destroy function frees the resources allocated for mutex .
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; }
See also: pthread_mutex_init() pthread_mutex_lock() pthread_mutex_trylock() pthread_mutex_unlock()
IMPORT_C int | pthread_mutex_lock | ( | pthread_mutex_t * | mutex | ) |
The pthread_mutex_lock function locks mutex . If the mutex is already locked, the calling thread will block until the mutex becomes available.
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 "); } }
See also: pthread_mutex_destroy() pthread_mutex_init() pthread_mutex_trylock() pthread_mutex_unlock()
IMPORT_C int | pthread_mutex_timedlock | ( | pthread_mutex_t * | mutex, |
const struct timespec * | abstime | |||
) |
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.
#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 ");
See also: pthread_mutex_destroy() pthread_mutex_init() pthread_mutex_lock() pthread_mutex_trylock() pthread_mutex_unlock()
IMPORT_C int | pthread_mutex_trylock | ( | pthread_mutex_t * | mutex | ) |
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.
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; }
See also: pthread_mutex_destroy() pthread_mutex_init() pthread_mutex_lock() pthread_mutex_unlock()
IMPORT_C int | pthread_mutex_unlock | ( | pthread_mutex_t * | mutex | ) |
If the current thread holds the lock on mutex , then the pthread_mutex_unlock function unlocks mutex .
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; }
See also: pthread_mutex_destroy() pthread_mutex_init() pthread_mutex_lock() pthread_mutex_trylock()
IMPORT_C int | pthread_condattr_init | ( | pthread_condattr_t * | ) |
Note: This description also covers the following functions - pthread_condattr_destroy()
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.
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; }
See also: pthread_cond_init()
IMPORT_C int | pthread_condattr_destroy | ( | pthread_condattr_t * | attr | ) |
Refer pthread_condattr_init() for the documentation
See also: pthread_cond_init()
IMPORT_C int | pthread_cond_init | ( | pthread_cond_t * | cond, |
const pthread_condattr_t * | ||||
) |
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.
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; } }
See also: pthread_cond_broadcast() pthread_cond_destroy() pthread_cond_signal() pthread_cond_timedwait() pthread_cond_wait()
Parameters | |
---|---|
represents the parameter attr |
IMPORT_C int | pthread_cond_destroy | ( | pthread_cond_t * | cond | ) |
The pthread_cond_destroy function frees the resources allocated by the condition variable cond .
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; } }
See also: pthread_cond_broadcast() pthread_cond_init() pthread_cond_signal() pthread_cond_timedwait() pthread_cond_wait()
IMPORT_C int | pthread_cond_timedwait | ( | pthread_cond_t * | cond, |
pthread_mutex_t * | mutex, | |||
const struct timespec * | abstime | |||
) |
The pthread_cond_timedwait function atomically blocks the current thread waiting on the condition, specified by the cond,and unblocks the mutex specified by the mutex.
The waiting thread unblocks only after another thread calls pthread_cond_signal,pthread_cond_broadcast with the same condition variable, or if the system time reaches the time specified in abstime,and the current thread reacquires the lock on the mutex.
struct testdata { pthread_mutex mutex; pthread_cond_t cond; }; static struct testdata td; int rc; struct timespec timeout; struct timeval curtime; if(pthread_mutex_lock(&td.mutex)!=0) { fprintf(stderr,"fail"); return -1; } if(gettimeofday(&curtime,NULL)!=0) { fprintf(stderr,"fail"); return -1; } timeout.tv_sec=curtime.tv_sec; timeout.tv_nsec=curtime.tv_nsec; timeout.tv_sec+=TIMEOUT; fprintf(stderr,"thread is waiting on the cond"); rc=pthread_cond_timedwait(&td.cond,&td.mutex,&timeout); if(rc!=0) { if(rc==ETIMEDOUT) { fprintf(stderr,"thread stops when time is out"); return -1; } else { fprintf(stderr,"pthread_cond_timedwait return"); return -1; } } fprintf(stderr,"thread wakened up"); pthread_mutex_unlock(&td.mutex);
See also: pthread_exit() pthread_join()
IMPORT_C int | pthread_cond_wait | ( | pthread_cond_t * | cond, |
pthread_mutex_t * | mutex | |||
) |
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 .
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;);
See also: pthread_cond_broadcast() pthread_cond_destroy() pthread_cond_init() pthread_cond_signal() pthread_cond_timedwait()
IMPORT_C int | pthread_cond_signal | ( | pthread_cond_t * | cond | ) |
The pthread_cond_signal function unblocks one thread waiting for the condition variable cond .
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; } }
See also: pthread_cond_broadcast() pthread_cond_destroy() pthread_cond_init() pthread_cond_timedwait() pthread_cond_wait()
IMPORT_C int | pthread_cond_broadcast | ( | pthread_cond_t * | cond | ) |
The pthread_cond_broadcast function unblocks all threads waiting for the condition variable cond .
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; }
See also: pthread_cond_destroy() pthread_cond_init() pthread_cond_signal() pthread_cond_timedwait() pthread_cond_wait()
typedef void(* | destructor_routine |
IMPORT_C int | pthread_key_create | ( | pthread_key_t * | key, |
destructor_routine | dest | |||
) |
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.
pthread_key_t keys; if(pthread_key_create(&keys;, NULL) != 0) { printf("Error: pthread_key_create() failed "); return -1; }
See also: pthread_getspecific() pthread_key_delete() pthread_setspecific()
IMPORT_C int | pthread_key_delete | ( | pthread_key_t | key | ) |
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.
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; }
See also: pthread_getspecific() pthread_key_create() pthread_setspecific()
IMPORT_C int | pthread_setspecific | ( | pthread_key_t | key, |
const void * | val | |||
) |
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.
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; } }
See also: pthread_getspecific() pthread_key_create() pthread_key_delete()
IMPORT_C void * | pthread_getspecific | ( | pthread_key_t | key | ) |
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.
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; }
See also: pthread_key_create() pthread_key_delete() pthread_setspecific()
IMPORT_C int | pthread_attr_getscope | ( | const pthread_attr_t * | attrib, |
int * | scope | |||
) |
See also: pthread_create()
Parameters | |
---|---|
scope | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_setscope | ( | pthread_attr_t * | attrib, |
int | conscope | |||
) |
See also: pthread_create()
Parameters | |
---|---|
conscope | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_setschedpolicy | ( | pthread_attr_t * | attrib, |
int | policy | |||
) |
See also: pthread_create()
Parameters | |
---|---|
policy | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_getschedpolicy | ( | const pthread_attr_t * | attrib, |
int * | policy | |||
) |
See also: pthread_create()
Parameters | |
---|---|
policy | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_getschedparam | ( | const pthread_attr_t * | attrib, |
struct sched_param * | param | |||
) |
See also: pthread_create()
Parameters | |
---|---|
param | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_setschedparam | ( | pthread_attr_t * | attrib, |
const struct sched_param * | param | |||
) |
See also: pthread_create()
Parameters | |
---|---|
param | Refer pthread_attr_init() for the documentation |
IMPORT_C int | pthread_attr_getinheritsched | ( | const pthread_attr_t * | attrib, |
int * | inheritsched | |||
) |
IMPORT_C int | pthread_attr_setinheritsched | ( | pthread_attr_t * | attrib, |
int | inheritsched | |||
) |
IMPORT_C int | pthread_getschedparam | ( | pthread_t | thr, |
int * | policy, | |||
struct sched_param * | param | |||
) |
Parameters | |
---|---|
param | Refer pthread_setschedparam() for the documentation |
IMPORT_C int | pthread_setschedparam | ( | pthread_t | th, |
int | policy, | |||
const struct sched_param * | param | |||
) |
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() and sched_get_priority_min() APIs) .
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 | |
---|---|
param | Note: This description also covers the following functions - pthread_getschedparam() |