Manage Thread-Stack Size in 64-bit UNIX


Challenge

Determine the default thread-stack size for a particular implementation of UNIX and adjust that size in your application. The operating system permits multiple threads of execution within a process's address space. Each thread is given its own execution context, including its own call/return stack. These stacks must be of sufficient size to store all of the likely call chains the thread might experience while it is running.

Because 64-bit applications will, in general, pass larger-sized items in function parameters than their 32-bit counterparts, migration to Itanium® architecture requires the assessment of stack and guard region sizes.


Solution

Extract the default sizes of the stack and guard regions put in place by the POSIX* threads facility, and modify the configuration settings if necessary. The POSIX threads facility, which is implemented by current UNIX operating systems, provides each created thread with a call/return stack. The stack size is defined by the specific implementation of UNIX.

If you allow the operating system to create and manage the thread's stack for you, a "stack guard" region is also allocated automatically. The guard region at the "top" of the allocated stack space will cause a runtime fault if it is accessed. The guard region is a way to inform you that the thread's stack has been exceeded and to prevent unobserved damage to data owned by other threads or data structures in the process.

You can extract the default sizes for the stack and guard regions by compiling and running the following piece of code:

pthread_attr_t thrAttr;

size_t sizeStack, sizeGuard;

pthread_attr_init(&thrAttr);

pthread_attr_getstacksize(&thrAttr, &sizeStack);

pthread_attr_getguardsize(&thrAttr, &sizeGuard);

 

If the values that are returned are not appropriate for your application, you must adjust the configuration settings before creating a thread, which is illustrated by the following piece of code:

pthread_t thrHandle;

pthread_attr_setstacksize(&thrAttr, 1000000); // a megabyte

pthread_attr_setguardsize(&thrAttr, PAGESIZE*8); // 8 OS pages

pthread_create(&thrHandle, &/*adjusted*/thrAttr, ...);

 


Source

Preparing Code for the IA-64 Architecture (Code Clean)

 


Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.