Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
3.  Thread Create Attributes Attributes Initialize Attributes pthread_attr_init(3THR)  Previous   Contents   Next 
   
 

Return Values

pthread_attr_init() returns zero after completing successfully. Any other return value indicates that an error occurred. If the following condition occurs, the function fails and returns the corresponding value.

 

ENOMEM

Returned when there is not enough memory to initialize the thread attributes object.

Destroy Attributes

pthread_attr_destroy(3THR)

Use pthread_attr_destroy(3THR) to remove the storage allocated during initialization. The attribute object becomes invalid.

Prototype:
int	pthread_attr_destroy(pthread_attr_t *tattr);
#include <pthread.h>

pthread_attr_t tattr;
int ret;

/* destroy an attribute */
ret = pthread_attr_destroy(&tattr); 

Return Values

pthread_attr_destroy() returns zero after completing successfully. Any other return value indicates that an error occurred. If the following condition occurs, the function fails and returns the corresponding value.

 

EINVAL

Indicates that the value of tattr was not valid.

Set Detach State

pthread_attr_setdetachstate(3THR)

When a thread is created detached (PTHREAD_CREATE_DETACHED), its thread ID and other resources can be reused as soon as the thread terminates. Use pthread_attr_setdetachstate(3THR) when the calling thread does not want to wait for the thread to terminate.

When a thread is created nondetached (PTHREAD_CREATE_JOINABLE), it is assumed that you will be waiting for it. That is, it is assumed that you will be executing a pthread_join() on the thread.

Whether a thread is created detached or nondetached, the process does not exit until all threads have exited. See "Finishing Up" for a discussion of process termination caused by premature exit from main().

Prototype:

int	pthread_attr_setdetachstate(pthread_attr_t *tattr,int detachstate);
#include <pthread.h>

pthread_attr_t tattr;
int ret;

/* set the thread detach state */
ret = pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED);

Note - When there is no explicit synchronization to prevent it, a newly created, detached thread can die and have its thread ID reassigned to another new thread before its creator returns from pthread_create().


For nondetached (PTHREAD_CREATE_JOINABLE) threads, it is very important that some thread join with it after it terminates--otherwise the resources of that thread are not released for use by new threads. This commonly results in a memory leak. So when you do not want a thread to be joined, create it as a detached thread.


Example 3-1 Creating a Detached Thread

#include <pthread.h>

pthread_attr_t tattr;
pthread_t tid;
void *start_routine;
void arg
int ret;

/* initialized with default attributes */
ret = pthread_attr_init()(&tattr);
ret = pthread_attr_setdetachstate()(&tattr,PTHREAD_CREATE_DETACHED);
ret = pthread_create()(&tid, &tattr, start_routine, arg);

Return Values

pthread_attr_setdetachstate() returns zero after completing successfully. Any other return value indicates that an error occurred. If the following condition occurs, the function fails and returns the corresponding value.

 

EINVAL

Indicates that the value of detachstate or tattr was not valid.

Get Detach State

pthread_attr_getdetachstate(3THR)

Use pthread_attr_getdetachstate(3THR) to retrieve the thread create state, which can be either detached or joined.

Prototype:

int	pthread_attr_getdetachstate(const pthread_attr_t *tattr,
    int *detachstate;
#include <pthread.h>

pthread_attr_t tattr;
int detachstate;
int ret;

/* get detachstate of thread */
ret = pthread_attr_getdetachstate (&tattr, &detachstate);

Return Values

pthread_attr_getdetachstate() returns zero after completing successfully. Any other return value indicates that an error occurred. If the following condition occurs, the function fails and returns the corresponding value.

 

EINVAL

Indicates that the value of detachstate is NULL or tattr is invalid.

Set Stack Guard Size

pthread_attr_setguardsize(3THR)

pthread_attr_setguardsize(3THR) sets the guardsize of the attr object.

The guardsize argument provides protection against overflow of the stack pointer. If a thread's stack is created with guard protection, the implementation allocates extra memory at the overflow end of the stack as a buffer against stack overflow of the stack pointer. If an application overflows into this buffer an error results (possibly in a SIGSEGV signal being delivered to the thread).

The guardsize attribute is provided to the application for two reasons:

  1. Overflow protection can potentially result in wasted system resources. An application that creates a large number of threads, and knows its threads will never overflow their stack, can save system resources by turning off guard areas.

  2. When threads allocate large data structures on stack, a large guard area may be needed to detect stack overflow.

If guardsize is zero, a guard area will not be provided for threads created with attr. If guardsize is greater than zero, a guard area of at least size guardsize bytes is provided for each thread created with attr. By default, a thread has an implementation-defined, non-zero guard area.

A conforming implementation is permitted to round up the value contained in guardsize to a multiple of the configurable system variable PAGESIZE (see PAGESIZE in sys/mman.h). If an implementation rounds up the value of guardsize to a multiple of PAGESIZE, a call to pthread_attr_getguardsize() specifying attr will store, in guardsize, the guard size specified in the previous call to pthread_attr_setguardsize().

#include <pthread.h>

int pthread_attr_setguardsize(pthread_attr_t *attr, size_t  guardsize);

Return Value

pthread_attr_setguardsize() fails if:

 

EINVAL

The argument attr is invalid, the argument guardsize is invalid, or the argument guardsize contains an invalid value.

Get Stack Guard Size

pthread_attr_getguardsize(3THR)

pthread_attr_getguardsize(3THR) gets the guardsize of the attr object.

A conforming implementation is permitted to round up the value contained in guardsize to a multiple of the configurable system variable PAGESIZE (see PAGESIZE in sys/mman.h). If an implementation rounds up the value of guardsize to a multiple of PAGESIZE, a call to pthread_attr_getguardsize() specifying attr will store, in guardsize, the guard size specified in the previous call to pthread_attr_setguardsize().

#include <pthread.h>

int pthread_attr_getguardsize(const pthread_attr_t *attr, 
										size_t  *guardsize);
 
 
 
  Previous   Contents   Next