1

I'm trying to find an appropriate value to set a boost::thread's stack size. I'm using the thread to perform recursive operations and was finding that default size was insufficient for my purposes, causing the stack to overflow.

I understand from the boost::thread::attributes documentation that the set_stack_size() function provides a portable interface to underlying platform-specific attributes.

boost::thread::attributes attributes;
std::cout << "DEFAULT: " << attributes.get_stack_size() << std::endl;

On my machine (an MBP running Yosemite / XCode 6.3.2), this prints:

DEFAULT: 524288

I've found that a value of 80,000,000 prevents crashes in all of my test cases. But, I'm not certain that this covers all possible cases and I'm hoping someone can provide some insight about the general design of this feature.

What is the cost of increasing this value? Or, to put it another way, does this setting directly control the amount of memory allocated at runtime or does it merely set some sort of potential upper-bound without actually allocating a huge block of memory from the get-go? From what I can tell so far, increasing this value does not seem to alter my program's memory use. Is this correct? And, would the cost differ by platform?

Thanks for your help!

pt3dNyc
  • 369
  • 1
  • 16
  • I would expect to see some difference. According to http://man7.org/linux/man-pages/man3/pthread_attr_setstacksize.3.html this sets the minimum number of bytes allocated per thread. – NG. Jun 22 '15 at 19:58
  • If you need a 80MB stack, then it's probably time to change from recursion to an iterative approach (using an external stack to hold your variables)... here's [another question](http://stackoverflow.com/questions/159590/way-to-go-from-recursion-to-iteration) about how to convert to an iterative approach.. – Buddy Jun 22 '15 at 21:07
  • Fair enough. In truth, I was greatly padding my test cases and would never actually need 80MB. Regardless, I should still pursue an iterative approach. Thanks for the link! – pt3dNyc Jun 22 '15 at 22:39

0 Answers0