Friday, April 26, 2013

pthread_attr_setstackaddr example c c++


NAME

pthread_attr_getstackaddr, pthread_attr_setstackaddr - get and set the stackaddr attribute

SYNOPSIS

[THR TSA] [Option Start] #include <pthread.h>

[Option End]
[OB] [Option Start] int pthread_attr_getstackaddr(const pthread_attr_t *restrict attr,
       void **restrict
 stackaddr);
int pthread_attr_setstackaddr(pthread_attr_t *
attr, void *stackaddr); [Option End]

DESCRIPTION

The pthread_attr_getstackaddr() and pthread_attr_setstackaddr() functions, respectively, shall get and set the thread creation stackaddrattribute in the attr object.
The stackaddr attribute specifies the location of storage to be used for the created thread's stack. The size of the storage shall be at least {PTHREAD_STACK_MIN}.

RETURN VALUE

Upon successful completion, pthread_attr_getstackaddr() and pthread_attr_setstackaddr() shall return a value of 0; otherwise, an error number shall be returned to indicate the error.
The pthread_attr_getstackaddr() function stores the stackaddr attribute value in stackaddr if successful.

ERRORS of (pthread_attr_getstackaddr, pthread_attr_setstackaddr)

These functions may fail if:
[EINVAL]
The value specified by attr does not refer to an initialized thread attribute object.
These functions shall not return an error code of [EINTR].
Example of (pthread_attr_getstackaddr, pthread_attr_setstackaddr)


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

#include <pthread.h>

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

stackbase = (void *) malloc(size);

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

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

/* only address specified in attribute tattr */
ret = pthread_create(&tid, &tattr, func, arg); 

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

#include <pthread.h>

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

int size = PTHREAD_STACK_MIN + 0x4000;
stackbase = (void *) malloc(size);

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

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

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

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