[Boost] [Thread] Query: Stack Size

Hi, How can I change the default stack size for each new thread being created? Secondly is there a mechanism to set the stack size while creation? I read somewhere that the default stack size is 8MB which is heavy for my application. Please suggest. Regards, Alap ________________________________ This Email may contain confidential or privileged information for the intended recipient (s) If you are not the intended recipient, please do not use or disseminate the information, notify the sender and delete it from your system. ______________________________________________________________________

Alapkumar Sinha <Alapkumar.Sinha@lntinfotech.com> writes:
How can I change the default stack size for each new thread being created?
On Windows, set the default stack size with the appropriate linker flags.
Secondly is there a mechanism to set the stack size while creation?
No. You can use platform-specific APIs to switch stacks after creation though.
I read somewhere that the default stack size is 8MB which is heavy for my application.
That's platform dependent. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

Hi, Thanks. I was checking the thread library implementation for pthread in the file boost_1_43_0/libs/thread/src/pthread/thread.cpp (Line number 185) and observed that boost trhead library calls pthread_create with attributes as NULL which means it takes default stack size. I have modified the code as below. Can someone tell me if this changes are fine or not? void thread::start_thread() { thread_info->self=thread_info; // [AKS] Addded code to set the stack size to 2 MB size_t mystacksize; pthread_attr_init(&attr1); //attr1 is a global variable mystacksize =2097152; pthread_attr_setstacksize (&attr1, mystacksize); //[AKS] End of change int const res = pthread_create(&thread_info->thread_handle, &attr1, &thread_proxy, thread_info.get()); if (res != 0) { thread_info->self.reset(); boost::throw_exception(thread_resource_error()); } } Thanks & Regards, Alap -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Anthony Williams Sent: Wednesday, July 28, 2010 7:01 PM To: boost@lists.boost.org Subject: Re: [boost] [Boost] [Thread] Query: Stack Size Alapkumar Sinha <Alapkumar.Sinha@lntinfotech.com> writes:
How can I change the default stack size for each new thread being created?
On Windows, set the default stack size with the appropriate linker flags.
Secondly is there a mechanism to set the stack size while creation?
No. You can use platform-specific APIs to switch stacks after creation though.
I read somewhere that the default stack size is 8MB which is heavy for my application.
That's platform dependent. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976 _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost ______________________________________________________________________ This Email may contain confidential or privileged information for the intended recipient (s) If you are not the intended recipient, please do not use or disseminate the information, notify the sender and delete it from your system. ______________________________________________________________________

Alapkumar Sinha <Alapkumar.Sinha@lntinfotech.com> writes:
I have modified the code as below. Can someone tell me if this changes are fine or not?
void thread::start_thread() { thread_info->self=thread_info; // [AKS] Addded code to set the stack size to 2 MB size_t mystacksize; pthread_attr_init(&attr1); //attr1 is a global variable mystacksize =2097152; pthread_attr_setstacksize (&attr1, mystacksize); //[AKS] End of change
int const res = pthread_create(&thread_info->thread_handle, &attr1, &thread_proxy, thread_info.get()); if (res != 0) { thread_info->self.reset(); boost::throw_exception(thread_resource_error()); }
}
It looks OK apart from the "attr1 is a global variable" part. That would introduce a race condition in starting threads. Don't do that, just make it local, or initialize it once (e.g. with call_once). Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
I read somewhere that the default stack size is 8MB which is heavy for my application.
It's only 1MB by default. And while that may seem pretty big, the vast majority of it is reserved-but-uncommitted memory -- in other words, it doesn't use any memory (only address space) until it's actually used. (See http://msdn.microsoft.com/en-us/library/ms686774%28VS.85%29.aspx for more details, if desired.) - -- Chad Nelson Oak Circle Software, Inc. * * * -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkxQQfgACgkQp9x9jeZ9/wSS5QCeMYC11OcV1Q5d1Y7T5ZtAQ9Id DNcAoIv0MM3oU3vGPV78QI9uKB6dXqHr =PpW/ -----END PGP SIGNATURE-----
participants (3)
-
Alapkumar Sinha
-
Anthony Williams
-
Chad Nelson