Name

pthread_attr_init ,pthread_attr_destroy ,pthread_attr_setstacksize
pthread_attr_getstacksize ,pthread_attr_setdetachstate ,pthread_attr_getdetachstate

pthread_attr_setschedparam ,pthread_attr_getschedparam ,pthread_attr_setschedpolicy

pthread_attr_getschedpolicy ,pthread_attr_setscope ,pthread_attr_setscope,

pthread_attr_getscope,thread attribute operations

- formatted output conversion

Library

libc_r.lib libpthread.lib libthr.lib

Synopsis

  #include <pthread.h>
  int pthread_attr_init (pthread_attr_t *attrib);
  int pthread_attr_destroy (pthread_attr_t *attrib);
  int pthread_attr_setstacksize (pthread_attr_t *attrib, size_t stkSize);
  int pthread_attr_getstacksize (const pthread_attr_t *attrib, size_t *stkSize);
  int pthread_attr_setdetachstate (pthread_attr_t *attrib, int detState);
  int pthread_attr_getdetachstate (const pthread_attr_t *attrib, int *detState);
  int pthread_attr_setschedparam (pthread_attr_t *attrib, const struct sched_param *param);
  int pthread_attr_getschedparam (const pthread_attr_t *attrib, struct sched_param *param);
  int pthread_attr_setschedpolicy (pthread_attr_t *attrib, int policy);
  int pthread_attr_getschedpolicy (const pthread_attr_t *attrib, int *policy);
  int pthread_attr_setscope (pthread_attr_t *attrib, int conscope);
  int pthread_attr_getscope (const pthread_attr_t *attrib, int *scope);

Return values

If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.

Detailed description

Thread attributes are used to specify parameters to pthread_create. One attribute object can be used in multiple calls to pthread_create, with or without modifications between calls.

The pthread_attr_init function initializes attrib with all the default thread attributes.

The pthread_attr_destroy function destroys attrib.

The pthread_attr_set* functions set the attribute that corresponds to each function name.

The pthread_attr_get* functions copy the value of the attribute that corresponds to each function name to the location pointed to by the second function parameter.


Limitation

The pthread_attr_setscope supports only PTHREAD_SCOPE_SYSTEM.

The pthread_attr_setschedpolicy supports only SCHED_RR.

pthread_join : Only one thread can join on one specific target thread.


Errors

The pthread_attr_init function will fail if:
[EINVAL]
  Invalid value for attrib.

The pthread_attr_destroy function will fail if:

[EINVAL]
  Invalid value for attrib.

The pthread_attr_setstacksize function will fail if:

[EINVAL]
  stacksize is less than PTHREAD_STACK_MIN. or if the attrib is not initialized or if attrib is NULL

The pthread_attr_getstacksize function will fail if:

[EINVAL]
  attrib is NULL or if the attrib is not initialized

The pthread_attr_setdetachstate function will fail if:

[EINVAL]
  Invalid value for detachstate or if the attrib is not initialized or if attrib is NULL

The pthread_attr_getdetachstate function will fail if:

[EINVAL]
  attrib is NULL or if the attrib is not initialized

The pthread_attr_setschedparam function will fail if:

[EINVAL]
  Invalid value for param. or if the attrib is not initialized or if attrib is NULL

The pthread_attr_getschedparam function may fail if:

[EINVAL]
  attrib does not refer to an initialized thread attribute or if attrib is NULL

The pthread_attr_setschedpolicy function will fail if:

[EINVAL]
  Invalid value for policy. or if the attrib is not initialized or if attrib is NULL

The pthread_attr_getschedpolicy function may fail if:

[EINVAL]
  attrib does not refer to an initialized thread attribute or if attrib is NULL

The pthread_attr_setscope function will fail if:

[EINVAL]
  Invalid value for contentionscope or if the attrib is not initialized or if attrib is NULL

The pthread_attr_getscope function may fail if:

[EINVAL]
  attrib does not refer to an initialized thread attribute or if attrib is NULL


Examples

/* ***************************************************** */
/* Example for pthread_attr_init & pthread_attr_destroy  */
/* ***************************************************** */
pthread_attr_t new_attr;
/* Initialize attribute */
if(pthread_attr_init(&new_attr) != 0)
{
  printf("Cannot initialize attribute object\n");
  return -1; /* or exit */
}
/* Create thread */
/* Destroy attribute */
if(pthread_attr_destroy(&new_attr) != 0)
{
  perror("Cannot destroy the attribute object\n");
  return -1; /* or exit*/
}

         

/* ***************************************************** */
/* Example for pthread_attr_getdetachstate               */
/* ***************************************************** */
pthread_attr_t new_attr;
int detach_state;
/* Initialize attribute */
if(pthread_attr_init(&new_attr) != 0)
{
  printf("Cannot initialize attribute object\n");
  return -1;
}
if(pthread_attr_getdetachstate(&new_attr, &detach_state) != 0)
{
  printf("Cannot get the state\n");
  return -1;
}
if(detach_state == PTHREAD_CREATE_JOINABLE)
{
  printf("State is joinable \n");
}
else
{
  printf("State is detached \n");
}
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_getschedpolicy               */
/* ***************************************************** */
pthread_attr_t new_attr;
int rc;
int policy;
/* Initialize attribute */
if(pthread_attr_init(&new_attr) != 0)
{
  printf("Cannot initialize attribute object\n");
  return -1; /* or exit */
}
rc = pthread_attr_getschedpolicy(new_attr, &policy);
if( rc != 0) {
  printf("pthread_attr_getschedpolicy failed\n");
  return -1;
}
switch(policy) {
 case SCHED_FIFO:
       printf("Scheduling policy: SCHED_FIFO 0);
       break;
 case SCHED_RR:
       printf("Scheduling policy: SCHED_RR \n");
       break;
 case SCHED_OTHER:
       printf("Scheduling policy: SCHED_OTHER \n");
       break;
}
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_getscope                     */
/* ***************************************************** */
pthread_attr_t new_attr;
int rc;
int scope;
/* Initialize attribute */
if(pthread_attr_init(&new_attr) != 0)
{
  printf("Cannot initialize attribute object\n");
  return -1; /* or exit */
}
rc = pthread_attr_getscope(new_attr, &scope);
if( rc != 0) {
  printf("pthread_attr_getscope failed");
  return -1;
}
switch(scope) {
 case SYSTEMSCOPE:
       printf("scope: System \n ");
       break;
 case PROCESSSCOPE:
       printf("scope: Process \n ");
       break;
}
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_getstacksize                 */
/* ***************************************************** */
pthread_attr_t new_attr;
size_t ssize;
int rc;
/* Initialize attribute */
if(pthread_attr_init(&new_attr) != 0)
{
  printf("Cannot initialize attribute object\n");
  return -1; /* or exit */
}
/* Get the default stack_size value */
rc = pthread_attr_getstacksize(&new_attr, &stack_size); 
if( rc != 0) {
  printf("pthread_attr_getstacksize failed");
  return -1;
}
printf("ssize = %lu\n", stack_size);
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_getschedparam               */
/* ***************************************************** */
pthread_attr_t         attr;
int                    rc=0;
struct sched_param     param;
struct sched_param     param2;
rc = pthread_attr_init(&attr);
if(rc != 0) {
  printf("pthread_attr_init failed\n");
  return -1;
}
param.sched_priority = 50;
rc = pthread_attr_setschedparam(&attr, & param);
if(rc != 0) {
  printf("pthread_attr_setschedparam failed\n");
  return -1;
}
rc = pthread_attr_getschedparam(&attr, & param2);
if(rc != 0) {
  printf("pthread_attr_getschedparam failed\n");
  return -1;
}
/* priority will be stored in param2.sched_priority */
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_setdetachstate               */
/* ***************************************************** */
pthread_attr_t new_attr;
int detach_state;
/* Initialize attribute */
if(pthread_attr_init(&new_attr) != 0)
{
  perror("Cannot initialize attribute object\n");
  return -1;
}
/* Set the attribute object to PTHREAD_CREATE_JOINABLE. */
if(pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_JOINABLE) != 0)
{
  perror("Error in pthread_attr_setdetachstate()\n");
  return -1;
}
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_setschedparam               */
/* ***************************************************** */
pthread_attr_t         attr;
int                    rc=0;
struct sched_param     param;
rc = pthread_attr_init(&attr);
if(rc != 0) {
  printf("pthread_attr_init failed\n");
  return -1;
}
param.sched_priority = 50;
rc = pthread_attr_setschedparam(&attr, & param);
if(rc != 0) {
  printf("pthread_attr_setschedparam failed\n");
  return -1;
}
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_setschedpolicy               */
/* ***************************************************** */
pthread_attr_t attr;
int rc;
int policy = SCHED_RR;
if(pthread_attr_init(&attr) != 0) {
  printf("Error on pthread_attr_init()\n");
  return -1;
}
 
if((rc=pthread_attr_setschedpolicy(&attr,policy)) != 0) {
      printf("Error on pthread_attr_setschedpolicy()\t rc=%d\n", rc);
      return -1;
}
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_setstacksize                 */
/* ***************************************************** */
pthread_attr_t attr;
int rc;
int policy = SCHED_RR;
if(pthread_attr_init(&attr) != 0) {
  printf("Error on pthread_attr_init()\n");
  return -1;
}
 
if((rc=pthread_attr_setstacksize(&attr,8192)) != 0) {
      printf("Error on pthread_attr_setstacksize()\t rc=%d\n", rc);
      return -1;
}
/* Create thread */
/* Destroy attribute */

         

/* ***************************************************** */
/* Example for pthread_attr_setscope                     */
/* ***************************************************** */
pthread_attr_t attr;
int rc;
/* Initialize attr */
rc = pthread_attr_init(&attr);
if( rc != 0) {
  perror("pthread_attr_init failed");
  return -1;
}
rc = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 
if (rc != 0 ) {
  perror("PTHREAD_SCOPE_SYSTEM is not supported");
  return -1;
}
/* Create thread */
/* Destroy attribute */

         

See also

pthread_create

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top