Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-06

Thread's stack size

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andreas Berger

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.

Reply by email

David W Noon
to Andreas Berger
Andreas Berger wrote 5 lines
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.

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.

Regards, Dave [RLU #314465] ======================================================================= (David W Noon) =======================================================================

Reply by email

Andreas Berger
to David W Noon
David W Noon wrote
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.

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
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.

Thanks for the tip. I'll reserve 16K. That should take care of various page sizes.

Regards,
Andreas

Reply by email

David W Noon
to Andreas Berger
Andreas Berger wrote 9 lines
David W Noon wrote: > 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. > 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.

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.]

Regards, Dave [RLU #314465] ======================================================================= (David W Noon) ======================================================================= -------------- next part -------------- A non-text attachment was scrubbed... Name: thread_test.zip Type: application/zip Size: 2932 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20090615/18d1e030/attachment-0001.zip>

Reply by email

Andreas Berger
to David W Noon

Thank you very much.

David W Noon wrote 13 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.]

True. Sorry.

Reply by email