pthread_attr_init()--Initialize Thread Attributes Object


  Syntax:
 #include <pthread.h>
 int pthread_attr_init(pthread_attr_t *attr);   
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_attr_init() function initializes a thread attributes object to the default thread attributes. The thread attributes object can be used in a call to pthread_create() to specify attributes of the new thread.


Authorities and Locks

None.


Parameters

attr
(Input/Output) The address of the thread attributes object to be initialized

Return Value

0
pthread_attr_init() was successful.
value
pthread_attr_init() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_attr_init() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]

The value specified for the argument is not correct.


Related Information


Example

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

void *threadfunc(void *parm)
{
  printf("Thread created using an default attributes\n");
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  pthread_attr_t        pta;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create a thread attributes object\n");
  rc = pthread_attr_init(&pta);
  checkResults("pthread_attr_init()\n", rc);

  printf("Create a thread using the attributes object\n");
  rc = pthread_create(&thread, &pta, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  printf("Create a thread using the default attributes\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  printf("Destroy thread attributes object\n");
  rc = pthread_attr_destroy(&pta);
  checkResults("pthread_attr_destroy()\n", rc);

  /* sleep() is not a very robust way to wait for the thread */
  sleep(5);

  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TAINI0
Create a thread attributes object
Create a thread using the attributes object
Create a thread using the default attributes
Destroy thread attributes object
Thread created using an default attributes
Thread created using an default attributes
Main completed


API introduced: V4R3

[ Back to top | Pthread APIs | APIs by category ]