Monday, December 13, 2010

thread relevant function in Linux

CREATE THREADS
---------------------
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);

WAIT FOR THE FINISH OF THREAD
-------------------------------------
int pthread_join(pthread_t thread, void **value_ptr);

PROTECTION OF SHARED RESOURCE AMONG MULTIPLE THREADS
------------------------------------------------------
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
If the mutex is already locked, the calling thread shall block
until the mutex becomes available.

THREAD SYNCH BY BLOCKING ON A CONDITION VARIABLE
--------------------------------------------------
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
Condition Variables: pthreads support condition variables, which
allow one thread to wait (sleep) for an event generated by any other thread.
This allows us to avoid the busy waiting problem.


THREAD SYNCH BY UNBLOCKING THREAD BLOCKED ON A CONDITION VARIABLE
------------------------------------------------------------------
int pthread_cond_broadcast(pthread_cond_t *cond); unblock all
int pthread_cond_signal(pthread_cond_t *cond); unblock at least one thread
The thread(s) that are unblocked shall contend for the mutex according to the
scheduling policy (if applicable), and as if each had called pthread_mutex_lock().



THREAD ATTRIBUTES SETTING
-----------------------------
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_init(pthread_attr_t *attr);

SET THREAD SCHEDULING POLICY
---------------------------------
int pthread_attr_getschedparam(const pthread_attr_t *restrict attr,
struct sched_param *restrict param);
int pthread_attr_setschedparam(pthread_attr_t *restrict attr,
const struct sched_param *restrict param);

int pthread_attr_getscope(const pthread_attr_t *restrict attr,
int *restrict contentionscope);
int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
contentionscope -- PTHREAD_SCOPE_SYSTEM,PTHREAD_SCOPE_PROCESS

int pthread_attr_getschedpolicy(const pthread_attr_t *restrict attr,
int *restrict policy);
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
policy -- SCHED_FIFO, SCHED_RR, SCHED_SPORADIC,and SCHED_OTHER

TERMINATE THREAD
----------------------
void pthread_exit(void *value_ptr);
If main() returns or any thread calls, exit()all threads are terminated



OTHER PASTRIES PTHREAD FUNCTIONS
-----------------------------------
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
int pthread_once(pthread_once_t *once_control,
void (*init_routine)(void));
pthread_once_t once_control = PTHREAD_ONCE_INIT;
void *pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);


Be aware of a list of the Posix thread-safe functions

No comments: