This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: stack size issues, multi-threading


Larry Meadows[SMTP:lfm@pgroup.com] wrote:
>1. How do I increase the stack size for an application? Is this done
>at link time, or can it be done at run time.
>
>2. How are stacks allocated for multi-threaded applications.

As Mikey said:

gcc -Wl,--heap,1024,--stack,4096 -o blah blah.c

for setting the heap and or stack of an executable.

Now for question 2:

You can set the stack size of threads as an argument to CreateThread
under Win32:

HANDLE CreateThread(
   LPSECURITY_ATTRIBUTES lpsa,
   DWORD cbStack,
   LPTHREAD_START_ROUTINE lpStartAddr,
   LPVOID lpvThreadParm,
   DWORD fdwCreate,
   LPDWORD lpIDThread);
  

Set cbStack to the number of bytes you wish to commit as an initial
stack size. You can also pass zero, in which case the same value as
was used for the primary thread (this is 0x1000, or 4 KB, and is not
affected by the --stack option) will be used.

It is my understanding that this size is not an upper limit on the
stack size of the thread, but only an initial size (and size to grow
by). This is based on the following passage from "Advanced Windows":

---quote---
The cbStack parameter specifies how much address space the thread is allowed to use for its own stack. Every thread owns its very own stack. When CreateProcess starts an application, it calls CreateThread to initialize the process's primary thread. For the cbStack parameter, CreateProcess uses the value stored inside the executable file. You can control this value using the linker's /STACK switch:
  
/STACK:[reserve] [,commit]
  
The reserve argument sets the amount of memory the system should reserve in the address space for the thread's stack. The default is 1 MB. The commit argument specifies the amount of reserved address space that should initially be committed to the stack. The default is 1 page. (See Chapter 6 for a discussion of reserving and committing memory.) As the code in your thread executes, it is quite possible that you'll require more than 1 page of memory. When your thread overflows its stack, an exception is generated. (See Chapter 14 for detailed information about handling exceptions.) The system catches the exception and commits another page (or whatever you specified for the commit argument) to the reserved space, which allows your thread's stacks to grow dynamically as needed.

When calling CreateThread you can pass 0 to the cbStack parameter. In this case, CreateThread creates a stack for the new thread using the commit argument embedded in the EXE file by the linker. The amount of reserved space is always 1 MB. The system sets a limit of 1 MB to stop functions that recurse endlessly.

---endquote---

The --stack option appears to change the "reserve" value (or upper
limit) on stack size. However, there seems to be no way to change
the upper limit on a thread's stack space (still, "1 MB of stack!?
What are you thinking!?").

It does seem that, other than possible efficiency issues, there's not much
point in setting the stack size, since it will grow dynamically anyway
(that is, if someone hasn't installed a buggy exception handler and messed
things up).

Hope this helps,
Colin.

-- Colin Peters - Saga Univ. Dept. of Information Science
-- colin@bird.fu.is.saga-u.ac.jp - finger for PGP public key
-- http://www.fu.is.saga-u.ac.jp/~colin/index.html
-- http://www.geocities.com/Tokyo/Towers/6162/

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]