Specifying thread stack size in pthreads

I want to increase the stack size of a thread created via pthread_create (). It seems like a way

int pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ); 

from pthread.h .

However, according to numerous online links ,

The stack should be properly aligned for use as a stack; for example, pthread_attr_setstack () may fail with [EINVAL] if (stackaddr and 0x7) is not 0.

My question is: can someone give an example of how to perform alignment? Is this (alignment) platform or implementation dependent?

Thanks in advance

+1
source share
2 answers

Never use pthread_attr_setstack . It has many fatal flaws, the worst of which is the inability to ever free or reuse the stack after creating a thread with it. (POSIX explicitly states that any attempt to do this results in undefined behavior.)

POSIX provides a much better function, pthread_attr_setstacksize , which allows you to query the stack size you need, but leaves the implementation with the responsibility of allocating and freeing the stack.

+11
source

Take a look at posix_memalign() .

It will allocate a block of memory of the requested alignment and size.

+1
source

All Articles