Threads library options

This section describes special attributes of threads, mutexes, and condition variables.

The POSIX standard for the threads library specifies the implementation of some parts as optional. All subroutines defined by the threads library API are always available. Depending on the available options, some subroutines may not be implemented. Unimplemented subroutines can be called by applications, but they always return the ENOSYS error code.

Stack attributes

A stack is allocated for each thread. Stack management is implementation-dependent. Thus, the following information applies only to AIX®, although similar features may exist on other systems.

The stack is dynamically allocated when the thread is created. Using advanced thread attributes, it is possible for the user to control the stack size and address of the stack. The following information does not apply to the initial thread, which is created by the system.

Stack size

The stack size option enables the control of the stacksize attribute of a thread attributes object. This attribute specifies the minimum stack size to be used for the created thread.

The stacksize attribute is defined in AIX. The following attribute and subroutines are available when the option is implemented:

The default value of the stacksize attribute is 96 KB. The minimum value of the stacksize attribute is 16 KB. If the assigned value is less than the minimum value, the minimum value is allocated.

In the AIX implementation of the threads library, a chunk of data, called user thread area, is allocated for each created thread. The area is divided into the following sections:
  • A red zone, which is both read-protected and write-protected for stack overflow detection. There is no red zone in programs that use large pages.
  • A default stack.
  • A pthread structure.
  • A thread structure.
  • A thread attribute structure.
Note: The user thread area described here has no relationship to the uthread structure used in the AIX kernel. The user thread area is accessed only in user mode and is exclusively handled by the threads library, whereas the uthread structure only exists within the kernel environment.

Stack address POSIX option

The stack address option enables the control of the stackaddr attribute of a thread attributes object. This attribute specifies the location of storage to be used for the created thread's stack.

The following attribute and subroutines are available when the option is implemented:
  • The stackaddr attribute of the thread attributes object specifies the address of the stack that will be allocated for a thread.
  • The pthread_attr_getstackaddr subroutine returns the value of the attribute.
  • and pthread_attr_setstackaddr subroutine sets the value.

If no stack address is specified, the stack is allocated by the system at an arbitrary address. If you must have the stack at a known location, you can use the stackaddr attribute. For example, if you need a very large stack, you can set its address to an unused segment, guaranteeing that the allocation will succeed.

If a stack address is specified when calling the pthread_create subroutine, the system attempts to allocate the stack at the given address. If it fails, the pthread_create subroutine returns EINVAL. Because the pthread_attr_setstackaddr subroutine does not actually allocate the stack, it only returns an error if the specified stack address exceeds the addressing space.

Priority scheduling POSIX option

The priority scheduling option enables the control of execution scheduling at thread level. When this option is disabled, all threads within a process share the scheduling properties of the process. When this option is enabled, each thread has its own scheduling properties. For local contention scope threads, the scheduling properties are handled at process level by a library scheduler, while for global contention scope threads, the scheduling properties are handled at system level by the kernel scheduler.

The folowing attributes and subroutines are available when the option is implemented:

  • The inheritsched attribute of the thread attributes object
  • The schedparam attribute of the thread attributes object and the thread
  • The schedpolicy attribute of the thread attributes objects and the thread
  • The contention-scope attribute of the thread attributes objects and the thread
  • The pthread_attr_getschedparam and pthread_attr_setschedparam subroutines
  • The pthread_getschedparam subroutine

Checking the availability of an option

Options can be checked at compile time or at run time. Portable programs should check the availability of options before using them, so that they need not be rewritten when ported to other systems.

Compile-time checking

When an option is not available, you can stop the compilation, as in the following example:
#ifndef _POSIX_THREAD_ATTR_STACKSIZE
#error "The stack size POSIX option is required"
#endif
The pthread.h header file also defines the following symbols that can be used by other header files or by programs:
_POSIX_REENTRANT_FUNCTIONS
Denotes that reentrant functions are required
_POSIX_THREADS
Denotes the implementation of the threads library

Run-time checking

The sysconf subroutine can be used to get the availability of options on the system where the program is executed. This is useful when porting programs between systems that have a binary compatibility, such as two versions of AIX.

The following list indicates the symbols that are associated with each option and that must be used for the Name parameter of the sysconf subroutine. The symbolic constants are defined in the unistd.h header file.

Stack address
_SC_THREAD_ATTR_STACKADDR
Stack size
_SC_THREAD_ATTR_STACKSIZE
Priority scheduling
_SC_THREAD_PRIORITY_SCHEDULING
Priority inheritance
_SC_THREAD_PRIO_INHERIT
Priority protection
_SC_THREAD_PRIO_PROTECT
Process sharing
_SC_THREAD_PROCESS_SHARED
To check the general options, use the sysconf subroutine with the following Name parameter values:
_SC_REENTRANT_FUNCTIONS
Denotes that reentrant functions are required.
_SC_THREADS
Denotes the implementation of the threads library.

Process sharing

AIX and most UNIX systems allow several processes to share a common data space, known as shared memory.The process-sharing attributes for condition variables and mutexes are meant to allow these objects to be allocated in shared memory to support synchronization among threads belonging to different processes. However, because there is no industry-standard interface for shared memory management, the process-sharing POSIX option is not implemented in the AIX threads library.

Threads data types

The following data types are defined for the threads library. The definition of these data types can vary between systems:
pthread_t
Identifies a thread
pthread_attr_t
Identifies a thread attributes object
pthread_cond_t
Identifies a condition variable
pthread_condattr_t
Identifies a condition attributes object
pthread_key_t
Identifies a thread-specific data key
pthread_mutex_t
Identifies a mutex
pthread_mutexattr_t
Identifies a mutex attributes object
pthread_once_t
Identifies a one-time initialization object

Limits and Default Values

The threads library has some implementation-dependent limits and default values. These limits and default values can be retrieved by symbolic constants to enhance the portability of programs:
  • The maximum number of threads per process is 512. The maximum number of threads can be retrieved at compilation time using the PTHREAD_THREADS_MAX symbolic constant defined in the pthread.h header file. If an application is compiled with the -D_LARGE_THREADS flag, the maximum number of threads per process is 32767.
  • The minimum stack size for a thread is 8 K. The default stack size is 96 KB. This number can be retrieved at compilation time using the PTHREAD_STACK_MIN symbolic constant defined in the pthread.h header file.
    Note: The maximum stack size is 256 MB, the size of a segment. This limit is indicated by the PTHREAD_STACK_MAX symbolic constant in the pthread.h header file.
  • The maximum number of thread-specific data keys is limited to 508. This number can be retrieved at compilation time using the PTHREAD_KEYS_MAX symbolic constant defined in the pthread.h header file.

Default attribute values

The default values for the thread attributes object are defined in the pthread.h header file by the following symbolic constants:
  • The default value for the DEFAULT_DETACHSTATE symbolic constant is PTHREAD_CREATE_DETACHED, which specifies the default value for the detachstate attribute.
  • The default value for the DEFAULT_JOINABLE symbolic constant is PTHREAD_CREATE_JOINABLE, which specifies the default value for the joinable state.
  • The default value for the DEFAULT_INHERIT symbolic constant is PTHREAD_INHERIT_SCHED, which specifies the default value for the inheritsched attribute.
  • The default value for the DEFAULT_PRIO symbolic constant is 1, which specifies the default value for the sched_prio field of the schedparam attribute.
  • The default value for the DEFAULT_SCHED symbolic constant is SCHED_OTHER, which specifies the default value for the schedpolicy attribute of a thread attributes object.
  • The default value for the DEFAULT_SCOPE symbolic constant is PTHREAD_SCOPE_LOCAL, which specifies the default value for the contention-scope attribute.