Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
3.  Thread Create Attributes Attributes About Stacks Building Your Own Stack  Previous   Contents   Next 
   
 

Set Stack Address

pthread_attr_setstackaddr(3THR)

pthread_attr_setstackaddr(3THR) sets the thread stack address.

The stackaddr attribute defines the base of the thread's stack. If this is set to non-null (NULL is the default) the system initializes the stack at that address.

Prototype:

int	pthread_attr_setstackaddr(pthread_attr_t *tattr,void *stackaddr);
#include <pthread.h>

pthread_attr_t tattr;
void *base;
int ret;

base = (void *) malloc(PTHREAD_STACK_MIN + 0x4000);

/* setting a new address */
ret = pthread_attr_setstackaddr(&tattr, base);

In the previous example, base contains the address for the stack that the new thread uses. If base is NULL, then pthread_create(3THR) allocates a stack for the new thread with at least PTHREAD_STACK_MIN bytes.

Return Values

pthread_attr_setstackaddr() 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

The value of base or tattr is incorrect.

This example shows how to create a thread with a custom stack address and size.

#include <pthread.h>

pthread_attr_t tattr;
pthread_t tid;
int ret;
void *stackbase;

/* initialized with default attributes */
ret = pthread_attr_init(&tattr);

/* setting the size of the stack */
ret = pthread_attr_setstacksize(&tattr, size);

/* setting the base address of the stack */
ret = pthread_attr_setstackaddr(&tattr, stackbase);

/* address and size specified */
ret = pthread_create(&tid, &tattr, func, arg);

Get Stack Address

pthread_attr_getstackaddr(3THR)

pthread_attr_getstackaddr(3THR) returns the thread stack address set by pthread_attr_setstackaddr().

Prototype:

int	pthread_attr_getstackaddr(pthread_attr_t *tattr,void * *stackaddr);
#include <pthread.h>

pthread_attr_t tattr;
void *base;
int ret;

/* getting a new address */
ret = pthread_attr_getstackaddr (&tattr, &base); 

Return Values

pthread_attr_getstackaddr() 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

The value or base or tattr is incorrect.

 
 
 
  Previous   Contents   Next