I thought I knew what the stack size argument in beginthread means. So my question is: why does this work?
#include <iostream>
#include <process.h>
using namespace std;
void huge_stack(int a, int b, int c, int d)
{
int i[100000] = {0};
cout << a << b << c << d << i[12333] << endl;
}
bool done = false;
void thread(void* p)
{
huge_stack(1,2,3,4);
done = true;
}
int main()
{
_beginthread(thread, 10, nullptr);
while(!done) {}
return 0;
}
I made sure I'm building in Debug mode, so the calls and arrays won't be optimized.
The reserve stack size is typically quite large... something like 1MB in Windows and 8MB in Linux. Note this is different than the commit size, which is where it starts, usually something along the lines of 4KB. The stack will grow automatically up to the reserve size. Also note on 32-bit systems, with large reserve stacks, you can run out of virtual address space fairly quickly with a few hundred threads (thus you can adjust this if needed using Windows link properties or Linux ulimit). Finally, for large objects, I'd recommend using the heap anyway.