Setting the default stack size on Linux globally for the program

Posted by wowus on Stack Overflow See other posts from Stack Overflow or by wowus
Published on 2010-06-17T00:22:59Z Indexed on 2010/06/17 0:32 UTC
Read the original article Hit count: 250

Filed under:
|
|
|

So I've noticed that the default stack size for threads on linux is 8MB (if I'm wrong, PLEASE correct me), and, incidentally, 1MB on Windows. This is quite bad for my application, as on a 4-core processor that means 64 MB is space is used JUST for threads! The worst part is, I'm never using more than 100kb of stack per thread (I abuse the heap a LOT ;)).

My solution right now is to limit the stack size of threads. However, I have no idea how to do this portably. Just for context, I'm using Boost.Thread for my threading needs. I'm okay with a little bit of #ifdef hell, but I'd like to know how to do it easily first.

Basically, I want something like this (where windows_* is linked on windows builds, and posix_* is linked under linux builds)

// windows_stack_limiter.c
int limit_stack_size()
{
    // Windows impl.
    return 0;
}

// posix_stack_limiter.c
int limit_stack_size()
{
    // Linux impl.
    return 0;
}

// stack_limiter.cpp
int limit_stack_size();
static volatile int placeholder = limit_stack_size();

How do I flesh out those functions? Or, alternatively, am I just doing this entirely wrong? Remember I have no control over the actual thread creation (no new params to CreateThread on Windows), as I'm using Boost.Thread.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c