Linux 如何设置 pthread 最大堆栈大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15435087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set pthread max stack size
提问by Ali
The API pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
is to set the minimumstack size (in bytes) allocated for the created thread stack.
APIpthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
用于设置为创建的线程堆栈分配的最小堆栈大小(以字节为单位)。
But how to set the maximumstack size?
但是如何设置最大堆栈大小?
Thanks
谢谢
回答by zz3599
I don't think there is an option to do that.
我认为没有办法做到这一点。
If you refer to <pthread.h>
, look for the constant PTHREAD_STACK_MIN
constant. That is the pre-determined stack size for all newly created threads by default.
如果您参考<pthread.h>
,请查找常量PTHREAD_STACK_MIN
常量。默认情况下,这是所有新创建线程的预定堆栈大小。
The behavior of pthread_attr_setstacksize
can be found here:
的行为pthread_attr_setstacksize
可以在这里找到:
The pthread_attr_setstacksize() function will fail if:
[EINVAL]
The value of stacksize is less than PTHREAD_STACK_MIN
or exceeds a system-imposed limit.
So, to answer your question precisely, no you cannot set the max stack size of a thread using POSIX library.
因此,要准确回答您的问题,您不能使用 POSIX 库设置线程的最大堆栈大小。
回答by Gabriel Southern
If you manage the allocation of memory for the stack yourself using pthread_attr_setstack
you can set the stack size exactly. So in that case the min is the same as the max. For example the code below illustrate a cases where the program tries to access more memory than is allocated for the stack and as a result the program segfaults.
如果您自己管理堆栈的内存分配,pthread_attr_setstack
您可以准确地设置堆栈大小。所以在这种情况下,最小值与最大值相同。例如,下面的代码说明了程序尝试访问的内存多于分配给堆栈的内存的情况,结果程序出现段错误。
#include <pthread.h>
#define PAGE_SIZE 4096
#define STK_SIZE (10 * PAGE_SIZE)
void *stack;
pthread_t thread;
pthread_attr_t attr;
void *dowork(void *arg)
{
int data[2*STK_SIZE];
int i = 0;
for(i = 0; i < 2*STK_SIZE; i++) {
data[i] = i;
}
}
int main(int argc, char **argv)
{
//pthread_attr_t *attr_ptr = &attr;
posix_memalign(&stack,PAGE_SIZE,STK_SIZE);
pthread_attr_init(&attr);
pthread_attr_setstack(&attr,&stack,STK_SIZE);
pthread_create(&thread,&attr,dowork,NULL);
pthread_exit(0);
}
If you rely on memory that is automatically allocated then you can specify a minimum amount but not a maximum. However, if the stack use of your thread exceeds the amount you specified then your program may segfault.
如果您依赖于自动分配的内存,那么您可以指定最小数量而不是最大数量。但是,如果您的线程的堆栈使用超过您指定的数量,那么您的程序可能会出现段错误。
Note that the man page for pthread_attr_setstacksize
says:
请注意,手册页pthread_attr_setstacksize
说:
A thread's stack size is fixed at the time of thread creation. Only the main thread can dynamically grow its stack.
A thread's stack size is fixed at the time of thread creation. Only the main thread can dynamically grow its stack.
To see an example of this program try taking a look at this link
要查看此程序的示例,请尝试查看此链接
You can experiment with the code segment that they provide and see that if you do not allocate enough stack space that it is possible to have your program segfault.
您可以试验他们提供的代码段,看看如果您没有分配足够的堆栈空间,您的程序可能会出现段错误。
回答by Randy Howard
The default stack size per thread is usually very large on most implementations of pthreads(). If you are running into issues where you need it larger than default though, you can try to increase it using pthread_attr_stacksize(), checking the return value as you do, and trying to find a larger value. The actual limit will probably be based upon per process address space limits on your specific system.
在 pthreads() 的大多数实现中,每个线程的默认堆栈大小通常非常大。如果您遇到需要它大于默认值的问题,您可以尝试使用 pthread_attr_stacksize() 增加它,像您一样检查返回值,并尝试找到更大的值。实际限制可能基于特定系统上的每个进程地址空间限制。
If you just want to find out what the default stack size is, you can use pthread_attr_getstacksize(). It is not going to grow larger than that, unless you tell it to by adjusting it yourself before you create the thread(s).
如果您只想了解默认堆栈大小,可以使用 pthread_attr_getstacksize()。它不会变得更大,除非您在创建线程之前通过自己调整它来告诉它。
Some code for adjusting stack sizes with pthreads is shown here https://stackoverflow.com/a/15356607/2159730
此处显示了一些使用 pthreads 调整堆栈大小的代码 https://stackoverflow.com/a/15356607/2159730
Note: the example in the link is for trying to minimize stack usage, and finding a minimum (which is dangerous, without a lot of testing). Sort of the opposite of what you seem to want to do, but the example may still be helpful to you. Just approach it differently.
注意:链接中的示例是为了尽量减少堆栈使用,并找到最小值(这是危险的,没有进行大量测试)。与您似乎想要做的事情相反,但该示例可能仍然对您有帮助。只是以不同的方式对待它。
In general, you should probably not need to raise the pthread stack size above the default. If you find you do, you probably should try understand why that is happening. It's fairly unusual.
通常,您可能不需要将 pthread 堆栈大小提高到默认值以上。如果你发现你这样做了,你可能应该尝试了解为什么会发生这种情况。这是相当不寻常的。
If you could explain what's actually happening in your own code, instead of a general question, other solutions might present themselves. I'm wondering if you are actually trying to reduce the per thread stack size, and just not explaining yourself clearly. If that's the case, the link might be helpful for that reason as well.
如果您可以解释您自己的代码中实际发生的情况,而不是一般性问题,其他解决方案可能会出现。我想知道您是否真的在尝试减少每个线程堆栈的大小,而只是没有清楚地解释自己。如果是这种情况,该链接也可能因该原因而有所帮助。