|
Q: My application maintains a large number of concurrent threads and needs to use a large amount of stack allocated data. How can I protect against overflowing my allocated stack space?A: Each Mac OS X process is launched with a default stack size of 8 Megabytes. This allocation is used exclusively for the main thread's stack needs. Each subsequent thread created is allocated its own default stack, the size of which differs depending on the threading API used. For example, the Mac OS X implementation of Pthreads defines a default stack size of 512 Kilobytes, while Carbon MPTasks are created with a 4 Kilobyte stack. A process can outgrow its allocated stack space by any of the following means:
Determining Process Stack LimitsSince certain system defaults and limits can change between releases of Mac OS X, it is useful to be able to determine the default process stack size for the system you are developing on. From the command line, Alternately, the system-defined default and maximum process stack sizes can be obtained using the Setting a Larger Stack at Link TimeIf you find your application is crashing because it is outgrowing the default 8 Megabyte stack allocation, you can raise its default by passing the IMPORTANT: Stack allocations must be done in multiples of the host's virtual memory page size. PowerPC and Intel-based Mac OS X systems have a page size of 4096 bytes. Setting The Stack Size to 16MB (0x1000000)Using Xcode: Add Using ld from a Makefile or command line:
Using gcc, pass link flags through to ld with
Dynamically Growing The Process Stack at RuntimeUsing the BSD system call Setting The Stack Size for an Individual ThreadYou can also increase the stack size allocated to threads as you create them. In Pthreads this is done by setting up a custom attribute and passing it to Listing 1: Setting a Pthread's Stack Size #include <limits.h> #include <pthread.h> #define REQUIRED_STACK_SIZE 1024*1024 int CreateThreadWithLargeStack (pthread_t *outThread, void *threadFunc, void *arg) { int err = 0; pthread_attr_t stackSizeAttribute; size_t stackSize = 0; /* Initialize the attribute */ err = pthread_attr_init (&stackSizeAttribute); if (err) return (err); /* Get the default value */ err = pthread_attr_getstacksize(&stackSizeAttribute, &stackSize); if (err) return (err); /* If the default size does not fit our needs, set the attribute with our required value */ if (stackSize < REQUIRED_STACK_SIZE) { err = pthread_attr_setstacksize (&stackSizeAttribute, REQUIRED_STACK_SIZE); if (err) return (err); } /* Create the thread with our attribute */ err = pthread_create (outThread, &stackSizeAttribute, threadFunc, arg); return (err); } Carbon MPTasksIf you are using Carbon Multiprocessing Services, you can set the stack size of an MPTask when it is created by passing a value other than 0 in the Cocoa NSThreadsAs of Mac OS X 10.5, the NSThread class includes the Document Revision History
Posted: 2008-02-20 |
|