在pthread中指定线程堆栈大小 [英] Specifying thread stack size in pthreads

查看:366
本文介绍了在pthread中指定线程堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想增加()通过在pthread_create创建的线程的堆栈大小。要走的路似乎是

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 ); 

pthreads.h中

然而,根据多个在线引用的,

However, according to multiple online references,

的stackaddr应适当对齐以用作一个堆栈;例如,pthread_attr_setstack()可能会失败,[EINVAL]如果(stackaddr&安培;为0x7)不为0

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?

在此先感谢

推荐答案

从不使用 pthread_attr_setstack 。它有很多致命的缺点,其中最严重的是,它是不可能的以往的游离或一个线程已经使用它创建后重复使用堆栈。 (POSIX明确规定,任何试图做未定义的行为如此效果。)

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提供了一个更好的功能, pthread_attr_setstacksize ,它允许你申请你所需要的堆栈大小,但保留执行与分配和释放堆栈中的责任。

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.

这篇关于在pthread中指定线程堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆