7

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

leiyc
  • 903
  • 11
  • 23
Ali
  • 167
  • 1
  • 2
  • 12
  • I just want to put in a comment about stack size: If you try to lookup "how much memory each thread eats up" and it ends up looking like your app takes some base memory + about 8MB per thread, you can probably relax. The memory is _not_ actually used/locked until you actually fill it up. It's often a bad idea to limit the stack size and there's no need for it in most cases either, except for embedded systems ofc with very limited resources. – Jite Mar 15 '13 at 21:43

3 Answers3

9

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.

Gabriel Southern
  • 9,602
  • 12
  • 56
  • 95
1

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.

Community
  • 1
  • 1
Randy Howard
  • 2,165
  • 16
  • 26
0

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.

zz3599
  • 647
  • 5
  • 20
  • thanks, it seems the param stacksize that i used is less than PTHREAD_STACK_MIN on MIPS. – Ali Mar 15 '13 at 15:12
  • 2
    You say: `The stack grows as needed for each thread automatically until you run out.` However, this is not correct in all cases. 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.` – Gabriel Southern Mar 15 '13 at 21:21