I notice that TThread calls BeginThread (Windows API) with the default stack size which is 1MB. Is there a way (cross platform) to reduce this size? I am writing an app that uses up to 200 threads that need absolutely no stack (I would still give them around 10K). That makes about 200MB of lost memory.
Mailing lists / lazarus / 2009-06
Thread's stack size
5 messages · 2 people ·
- AAndreas Berger 3
- DDavid W Noon 2
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Andreas Berger wrote 5 lines
You need to create a custom class that inherits from TThread. In the constructor of your class you can specify a stack size using an "inherited" statement. If you want some sample code, just ask.
Note also that stack sizes go in multiples of 4KiB (or whatever the page size is on the target platform). Moreover, there are no threads that "need absolutely no stack", as there can be compiler-generated temporary locations, as well as register save areas, that must be placed on the stack. So, even if you don't create any local variables, the compiler will expect some stack space to be available.
David W Noon wroteYou need to create a custom class that inherits from TThread. In the constructor of your class you can specify a stack size using an "inherited" statement. If you want some sample code, just ask.
Yes, since I would need to call the derived TThread's inherited constructor and not TThread itself. I wouldn't know how to do that. I would especially be interrested in the Linux code since I don't really know the Linux API.
Show quoted text 7 lines
Thanks for the tip. I'll reserve 16K. That should take care of various page sizes.
Regards,
Andreas
Andreas Berger wrote 9 lines
Attached is a zip archive of some source code. It also includes some declarations of the raw Linux/POSIX API, so it will also show you how to incorporate [g]libc functions into your Pascal code.
Note that you need to ensure that your glibc build includes NPTL (Native POSIX Threading Library), as most of the modern API calls will not work with the old (and rather trashy) LinuxThreads library.
If you need any explanations, just ask.
[Just as an aside, this thread would have been more on-topic in the fpc-user mailing list, as it isn't Lazarus-specific.]
Thank you very much.
David W Noon wrote 13 lines
True. Sorry.
