Each thread has a set of attributes when it is created such as:
- Initialize attributes
- Destroy attributes
- Detach state
- Scheduling parameters
- Stack size
- Stack Address
- Thread scope
- When a thread is created by pthread_create() API, and the attribute parameter is NULL, it is created with the default values for the above attributes.
- The threads attributes are specified only during the creation time, they cannot be altered once it is in use.
- But any thread can be created with values different from that of default values.
Initialized Attributes:
The pthread_attr_init() API can be used to initialize the object attributes to their default values. The storage is allocated by the thread system during execution.
/* Protocol for initializing the attributes */
int pthread_attr_init (pthread_attr_t *attr);
#include <pthread.h>
pthread_attr_t attr;
int ret = pthread_attr_init (&attr);
Return values:
It returns 0 on success and non zero value in case of any failure.
Few of the important attributes with their default value are:
Attributes | Value | Result |
scope | PTHREAD_SCOPE_PROCESS | New thread has a process scope not system scope (PTHREAD_SCOPE_SYSTEM) |
detachstate | PTHREAD_CREATE_JOINABLE | Exit status and thread is preserved after the thread terminates. |
stackaddr | NULL | New threads has system-allocated stack address |
stacksize | 1 Megabyte | New thread has system-defined stack size. |
inheiritsched | PTHREAD_INHERIT_SCHED | New thread inherit the parent thread scheduling priority |
priority | New thread inherit the parent thread priority | |
schedpolicy | SCHED_OTHERS | New thread run until preempted by a higher-priority thread or until they block or yield |
Destroy Attributes
The thread attributes can be destroyed by using pthread_attr_destroy() API
/* Protocol for destroying the attributes */
int pthread_attr_destroy (pthread_attr_t *attr);
#include <pthread.h>
pthread_attr_t attr;
int res = pthread_attr_destroy (&attr);
Return values:
It returns 0 on success and non-zero in case of failure.
Detached state
- By default, a thread is created in a joinable mode that its exit status and an identifier exist in a system even when a thread is terminated.
- Thus pthread_join() must be used to get the exit status and release all the resources of the tread so that it can be reuse.
- This default behavior can be changed to a detached state by using pthread_attr_setdetachstate() API
- Refer to pthread_detach() for detaching a thread.
/* Protocol for setting the detachstate */
int pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate);
/*Protocol for getting the detachstate */
int pthreead_attr_getdetachstate(pthread_attr_t *attr, int detahcstate);
Sample Code:
#include <pthread.h>
pthread_attr_t attr;
pthread_t th_id;
int res, detach_state;
/* Initialize the attribute */
res = pthread_attr_init (&attr);
/*Set the detach state */
res = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACH);
/*Create the thread with detach state */
res = pthread_create (&th_id, &attr, fun, NULL);
/*Get the detach state value */
res = pthread_attr_getdetachstate(&attr, &detach_state);
Return values:
It returns 0 on success and non-zero on failure.
Scheduling Attributes:
- The POSIX thread specifies three attributes for scheduling policy:
- SCHED_FIFO
- SCHED_RR
- SCHED_OTHERS
- SCHED_OTHERS is the default scheduling policy attribute.
/*Protocol for setting the scheduling policy */
int pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
/*Protocol for getting the scheduling policy */
int pthread_attr_getschedpolicy (pthread_attr_t *attr, int *policy);
Sample Code:
#include <pthread.h>
pthread_attr_t attr;
int policy, res;
pthread_t th_id;
/* Initialize the attribute *
res = pthread_attr_init (&attr);
/* Get the default scheduling policy */
res = pthread_attr_getschedpolicy (&attr, &policy);
printf("\n Default policy is %d\n", policy);
/* Setting a new scheduling policy */
res = pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
res = pthread_create (&th_id, &attr, fun, NULL);
/* Fetching the new value */
res = pthread_attr_getschedpolicy (&attr, &policy);
printf("\n New policy is %d\n", policy);
Return values:
It returns 0 on success and non-zero in case of failure
Relevant Post:
- POSIX Thread APIs
- Thread creates attributes
- Thread attributes(TCB)
- Characteristics of a Thread
- Odd-even problem
Reference:
https://docs.oracle.com/cd/E19455-01/806-5257/6je9h032l/index.html#attrib-76485
Categories: Operating system (OS)
Leave a Reply