pthread_attr_setstacksize() — Set the stacksize attribute object

Standards

Standards / Extensions C or C++ Dependencies

POSIX.4a
Single UNIX Specification, Version 3

both

POSIX(ON)

Format

#define _OPEN_THREADS
#include <pthread.h>

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
SUSV3:
#define _UNIX03_THREADS
#include <pthread.h>

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);

General description

Sets the stacksize, in bytes, for the thread attribute object, attr. stacksize is the initial stack size. Other stack characteristics, like stack increment size, are inherited from the STACK64/THREADSTACK64 runtime option.

You can use a thread attribute object to manage the characteristics of threads in your application. It defines the set of values to be used for the thread during its creation. By establishing a thread attribute object, you can create many threads with the same set of characteristics, without defining those characteristics for each thread. You can define more than one thread attribute object.

Usage notes

  1. An XPLINK application uses two stacks, an upward-growing stack and a downward-growing stack. The "stacksize" refers to the size of the downward-growing stack.
  2. When using Single UNIX Specification, Version thread support, the minimum stacksize in 31-bit is 4096 (4K) and in 64-bit 1048576 (1M). In addition, the system will allocate an equivalent-sized guardpage. There is no specified maximum stacksize. If more storage is requested than the system can satisfy at pthread creation, then pthread_create() will fail and return EINVAL.

Returned value

If successful, pthread_attr_setstacksize() returns 0.

If unsuccessful, pthread_attr_setstacksize() returns -1.

Error Code
Description
EINVAL
The value of stacksize is less than PTHREAD_STACK_MIN, or the value specified by attr does not refer to an initialized thread attribute object.

Special behavior for Single UNIX Specification, Version 3:

If unsuccessful, pthread_attr_setstacksize() returns an error number to indicate the error.

Example

CELEBP12
⁄* CELEBP12 *⁄                                   
#define _OPEN_THREADS                                                           
#include <stdio.h>                                                              
#include <pthread.h>                                                            
                                                                                
void *thread1(void *arg)                                                        
{                                                                               
   printf("hello from the thread\n");                                           
   pthread_exit(NULL);                                                          
}                                                                               
                                                                                
int main()                                                                      
{                                                                               
   int            rc, stat;                                                     
   size_t         s1;                                                           
   pthread_attr_t attr;                                                         
   pthread_t      thid;                                                         
                                                                                
   rc = pthread_attr_init(&attr);                                               
   if (rc == -1) {                                                              
      perror("error in pthread_attr_init");                                     
      exit(1);                                                                  
   }                                                                            
                                                                                
   s1 = 4096;                                                                   
   rc = pthread_attr_setstacksize(&attr, s1);                                   
   if (rc == -1) {                                                              
      perror("error in pthread_attr_setstacksize");                             
      exit(2);                                                                  
   }                                                                            
                                                                                
   rc = pthread_create(&thid, &attr, thread1, NULL);                            
   if (rc == -1) {                                                              
      perror("error in pthread_create");                                        
      exit(3);                                                                  
   }                                                                            
                                                                                
   rc = pthread_join(thid, (void *)&stat);                                      
   exit(0);                                                                     
}                                                                               

Related information