Subj : Re: stack information / stack overflow To : comp.programming.threads From : Sean Burke Date : Tue Mar 22 2005 08:20 pm "Michael Schmidt" writes: > Is there a portable way to retrieve information about a thread's stack size > and start address at runtime ? No. > How can I handle a stack overflow in a thread ? What I have done is to, in the thread start function, record the stack region's base address and size in thread-local variables (pthread_get/setspecific()). In recursive code, the current stack pointer is checked against these limits, and the function fails when the end of the stack region is approached, e.g. int foo() { char c; if ((&c - STACK_MARGIN) < pthread_getspecific(StackLimitKey)) { fprintf(stderr, "foo failed due to excessive recursion."); return NULL; } This is a kludge. Note that it assumes that the stack grows downward, that pointers can be compared, etc, etc. You must test it on every architecture and OS that you use it on. The STACK_MARGIN must be adequate for any code (like the fprintf() above) that runs when the limit is reached, (including signal handlers if the OS doesn't run them on a separate stack). -SEan .