开发者

Specifying thread stack size in pthreads

I want to increase the stack size of a thread created through pthread_create(). The way to go seems to be

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

from 开发者_运维问答pthread.h.

However, according to multiple online references,

The stackaddr shall be aligned appropriately to be used as a stack; for example, pthread_attr_setstack() may fail with [EINVAL] if ( stackaddr & 0x7) is not 0.

My question: could someone provide an example of how to perform the alignment? Is it (the alignment) platform or implementation dependent?

Thanks in advance


Never use pthread_attr_setstack. It has a lot of fatal flaws, the worst of which is that it is impossible to ever free or reuse the stack after a thread has been created using it. (POSIX explicitly states that any attempt to do so results in undefined behavior.)

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


Look into posix_memalign().

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜