Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
  Previous   Contents   Next 
   
 
Chapter 3

Thread Create Attributes

The previous chapter covered the basics of threads creation using default attributes. This chapter discusses setting attributes at thread creation time.

Note that only pthreads uses attributes and cancellation, so the API covered in this chapter is for POSIX threads only. Otherwise, the functionality for Solaris threads and pthreads is largely the same. (See Chapter 9, Programming With Solaris Threads, for more information about similarities and differences.)

Attributes

Attributes are a way to specify behavior that is different from the default. When a thread is created with pthread_create(3THR) or when a synchronization variable is initialized, an attribute object can be specified. The defaults are usually sufficient.

An attribute object is opaque, and cannot be directly modified by assignments. A set of functions is provided to initialize, configure, and destroy each object type.

Once an attribute is initialized and configured, it has process-wide scope. The suggested method for using attributes is to configure all required state specifications at one time in the early stages of program execution. The appropriate attribute object can then be referred to as needed.

Using attribute objects has two primary advantages.

  • First, it adds to code portability.

    Even though supported attributes might vary between implementations, you need not modify function calls that create thread entities because the attribute object is hidden from the interface.

    If the target port supports attributes that are not found in the current port, provision must be made to manage the new attributes. This is an easy porting task though, because attribute objects need only be initialized once in a well-defined location.

  • Second, state specification in an application is simplified.

    As an example, consider that several sets of threads might exist within a process, each providing a separate service, and each with its own state requirements.

    At some point in the early stages of the application, a thread attribute object can be initialized for each set. All future thread creations will then refer to the attribute object initialized for that type of thread. The initialization phase is simple and localized, and any future modifications can be made quickly and reliably.

Attribute objects require attention at process exit time. When the object is initialized, memory is allocated for it. This memory must be returned to the system. The pthreads standard provides function calls to destroy attribute objects.

Initialize Attributes

pthread_attr_init(3THR)

Use pthread_attr_init(3THR) to initialize object attributes to their default values. The storage is allocated by the thread system during execution.

Prototype:

int pthread_attr_init(pthread_attr_t *tattr);
#include <pthread.h>

pthread_attr_t tattr;
int ret;

/* initialize an attribute to the default value */
ret = pthread_attr_init(&tattr);

Table 3-1 shows the default values for attributes (tattr) .

Table 3-1 Default Attribute Values for tattr

Attribute

Value

Result

scope

PTHREAD_SCOPE_PROCESS

New thread is unbound - not permanently attached to LWP.

detachstate

PTHREAD_CREATE_JOINABLE

Exit status and thread are preserved after the thread terminates.

stackaddr

NULL

New thread has system-allocated stack address.

stacksize

0

New thread has system-defined stack size.

priority

0

New thread has priority 0.

inheritsched

PTHREAD_EXPLICIT_SCHED

New thread does not inherit parent thread scheduling priority.

schedpolicy

SCHED_OTHER

New thread uses Solaris-defined fixed priorities for synchronization object contention; threads run until preempted or until they block or yield.

 
 
 
  Previous   Contents   Next