Thread Create Attributes:

Each thread has a set of attributes when it is created such as:

  • 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
scopePTHREAD_SCOPE_PROCESSNew thread has a process scope not system scope (PTHREAD_SCOPE_SYSTEM)
detachstatePTHREAD_CREATE_JOINABLEExit status and thread is preserved after the thread terminates.
stackaddrNULLNew threads has system-allocated stack address
stacksize1 MegabyteNew thread has system-defined stack size.
inheiritschedPTHREAD_INHERIT_SCHEDNew thread inherit the parent thread scheduling priority
priorityNew thread inherit the parent thread priority
schedpolicySCHED_OTHERSNew 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_RR
  • 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:


Categories: Operating system (OS)

2 replies


  1. Odd Even problem by two threads - Tech Access
  2. Index of Operating System - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: