First, get a unique value for the key, mywin_key. This key is used to identify the thread-specific class of data. So, the first thread to call make_mywin() eventually calls pthread_key_create(), which assigns to its first argument a unique key. The second argument is a destructor function that is used to deallocate a thread's instance of this thread-specific data item once the thread terminates.
The next step is to allocate the storage for the caller's instance of this thread-specific data item. Having allocated the storage, a call is made to the create_window() routine, which sets up a window for the thread and sets the storage pointed to by win to refer to it. Finally, a call is made to pthread_setspecific(), which associates the value contained in win (that is, the location of the storage containing the reference to the window) with the key.
When a thread terminates, calls are made to the destructor functions that were set up in pthread_key_create(). Each destructor function is called only if the terminating thread established a value for the key by calling pthread_setspecific().
Get the Thread Identifier
Use pthread_self(3THR) to get the thread identifier of the calling thread.
Prototype: pthread_t pthread_self(void);
#include <pthread.h> pthread_t tid; tid = pthread_self();
pthread_self() returns the thread identifier of the calling thread.
Compare Thread IDs
Use pthread_equal(3THR) to compare the thread identification numbers of two threads.
Prototype: int pthread_equal(pthread_t tid1, pthread_t tid2);
#include <pthread.h> pthread_t tid1, tid2; int ret; ret = pthread_equal(tid1, tid2);
pthread_equal() returns a nonzero value when tid1 and tid2 are equal; otherwise, zero is returned. When either tid1 or tid2 is an invalid thread identification number, the result is unpredictable.
Use pthread_once(3THR) to call an initialization routine the first time pthread_once(3THR) is called. Subsequent calls to pthread_once() have no effect.
Prototype: int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
#include <pthread.h> pthread_once_t once_control = PTHREAD_ONCE_INIT; int ret; ret = pthread_once(&once_control, init_routine);
The once_control parameter determines whether the associated initialization routine has been called.
pthread_once() returns zero after completing successfully. Any other return value indicates that an error occurred. When the following condition occurs, pthread_once() fails and returns the corresponding value.
once_control or init_routine is NULL.
Yield Thread Execution
Use sched_yield(3RT) to cause the current thread to yield its execution in favor of another thread with the same or greater priority.
Prototype: int sched_yield(void);
#include <sched.h> int ret; ret = sched_yield();
sched_yield() returns zero after completing successfully. Otherwise -1 is returned and errno is set to indicate the error condition.
sched_yield(3RT) is not supported in this implementation.
Set the Thread Priority
Use pthread_setschedparam(3THR) to modify the priority of an existing thread. This function has no effect on scheduling policy.
Prototype: int pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param);
#include <pthread.h> pthread_t tid; int ret; struct sched_param param; int priority; /* sched_priority will be the priority of the thread */ sched_param.sched_priority = priority; /* only supported policy, others will result in ENOTSUP */ policy = SCHED_OTHER; /* scheduling parameters of target thread */ ret = pthread_setschedparam(tid, policy, ¶m);
pthread_setschedparam() returns zero after completing successfully. Any other return value indicates that an error occurred. When either of the following conditions occurs, the pthread_setschedparam() function fails and returns the corresponding value.
The value of the attribute being set is not valid.
An attempt was made to set the attribute to an unsupported value.
Get the Thread Priority
pthread_getschedparam(3THR) gets the priority of the existing thread.
Prototype: int pthread_getschedparam(pthread_t tid, int policy, struct schedparam *param);
#include <pthread.h> pthread_t tid; sched_param param; int priority; int policy; int ret; /* scheduling parameters of target thread */ ret = pthread_getschedparam (tid, &policy, ¶m); /* sched_priority contains the priority of the thread */ priority = param.sched_priority;