>> For platform specific way best solution is to read where bottom of stack is >> and allocate only when at least say 32768 bytes are left. >> > > And when knowing stack boundaries I could also recognize stack pointer > by single comparison. > > It needs to define _STACK_TOP,_STACK_CUP, _STACK_SIZE macros for stack > parameters. > I can find them by pthread_attr_getstack but this call is slow.
I think this approach has two issues. 1) On LInux, RLIMIT_STACK doesn't affect to main thread. so there is no reliable stack limit detection way. Think mmap vs stack expansion race case for example. 2) setcontext/getcontext family provide to implement userland thread (it is sometimes called fiber or green thread). so pthread_attr_getstack is not suitable for getting generic stack knowledge. I suppose original hardcoded 4096 boundary is best. Thank you.