Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
2.  Basic Threads Programming The Threads Library Get Thread-Specific Data pthread_getspecific(3THR)  Previous   Contents   Next 
   
 

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.

After this, whenever this thread calls pthread_getspecific(), passing the global key, it gets the value that was associated with this key by this thread when it called pthread_setspecific().

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

pthread_self(3THR)

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();

Return Values

pthread_self() returns the thread identifier of the calling thread.

Compare Thread IDs

pthread_equal(3THR)

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);

Return Values

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.

Initializing Threads

pthread_once(3THR)

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.

Return Values

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.

 

EINVAL

once_control or init_routine is NULL.

Yield Thread Execution

sched_yield(3RT)

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();

Return Values

sched_yield() returns zero after completing successfully. Otherwise -1 is returned and errno is set to indicate the error condition.

 

ENOSYS

sched_yield(3RT) is not supported in this implementation.

Set the Thread Priority

pthread_setschedparam(3THR)

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, &param); 

Return Values

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.

 

EINVAL

The value of the attribute being set is not valid.

 

ENOTSUP

An attempt was made to set the attribute to an unsupported value.

Get the Thread Priority

pthread_getschedparam(3THR)

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, &param);

/* sched_priority contains the priority of the thread */
priority = param.sched_priority; 
 
 
 
  Previous   Contents   Next