The API pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
is to set the minimum stack size (in bytes) allocated for the created thread stack.
But how to set the maximum stack size?
Thanks
The API pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
is to set the minimum stack size (in bytes) allocated for the created thread stack.
But how to set the maximum stack size?
Thanks
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.
#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:
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.
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.
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).
Some code for adjusting stack sizes with pthreads is shown here 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.
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.
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.
The behavior of pthread_attr_setstacksize
can be found here:
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.