![]() |
![]() |
| ||||||
About StacksTypically, thread stacks begin on page boundaries and any specified size is rounded up to the next page boundary. A page with no access permission is appended to the overflow end of the stack so that most stack overflows result in sending a SIGSEGV signal to the offending thread. Thread stacks allocated by the caller are used as is. When a stack is specified, the thread should also be created PTHREAD_CREATE_JOINABLE. That stack cannot be freed until the pthread_join(3THR) call for that thread has returned, because the thread's stack cannot be freed until the thread has terminated. The only reliable way to know if such a thread has terminated is through pthread_join(3THR). Generally, you do not need to allocate stack space for threads. The threads library allocates 1 megabyte (for 32 bit) or 2 megabytes (for 64 bit) of virtual memory for each thread's stack with no swap space reserved. (The library uses the MAP_NORESERVE option of mmap() to make the allocations.) Each thread stack created by the threads library has a red zone. The library creates the red zone by appending a page to the overflow end of a stack to catch stack overflows. This page is invalid and causes a memory fault if it is accessed. Red zones are appended to all automatically allocated stacks whether the size is specified by the application or the default size is used. Note - Because runtime stack requirements vary, you should be absolutely certain that the specified stack will satisfy the runtime requirements needed for library calls and dynamic linking. There are very few occasions when it is appropriate to specify a stack, its size, or both. It is difficult even for an expert to know if the right size was specified. This is because even a program compliant with ABI standards cannot determine its stack size statically. Its size is dependent on the needs of the particular runtime environment in which it executes. Building Your Own StackWhen you specify the size of a thread stack, be sure to account for the allocations needed by the invoked function and by each function called. The accounting should include calling sequence needs, local variables, and information structures. Occasionally you want a stack that is a bit different from the default stack. An obvious situation is when the thread needs more than the default stack size. A less obvious situation is when the default stack is too large. You might be creating thousands of threads and not have enough virtual memory to handle the gigabytes of stack space that this many default stacks require. The limits on the maximum size of a stack are often obvious, but what about the limits on its minimum size? There must be enough stack space to handle all of the stack frames that are pushed onto the stack, along with their local variables, and so on. You can get the absolute minimum limit on stack size by calling the macro PTHREAD_STACK_MIN, which returns the amount of stack space required for a thread that executes a NULL procedure. Useful threads need more than this, so be very careful when reducing the stack size.
Set Stack Addresspthread_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.
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 Valuespthread_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.
This example shows how to create a thread with a custom stack address and size.
Get Stack Addresspthread_attr_getstackaddr(3THR)pthread_attr_getstackaddr(3THR) returns the thread stack address set by pthread_attr_setstackaddr().
Return Valuespthread_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.
| ||||||
| ||||||