[osg-users] [patch] PThread.c++ creation error, lower stack size limit
Robert Osfield
robert.osfield at gmail.com
Fri Nov 20 02:28:01 PST 2009
Thanks David, changes look sound, now merged and submitted to svn/trunk.
On Fri, Oct 16, 2009 at 1:31 PM, David Fries <david at fries.net> wrote:
> This was against 2.8.1, but I see the trunk is identical.
> Any issues with using PTHREAD_STACK_MIN?
> http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getstacksize.html
>
>
> I was trying to create a lot of threads under 32 bit Linux, but could
> only create 376, then the program would hang.
> 376 * 8MB stack per thread = 3008 MB
> The stack size allocated per thread blew the process address stack.
> To get more threads you have to specify a smaller per thread stack,
> but while the Thread::start says it will limit the stack size to the
> smallest allowable stack size, it won't let it be smaller than the
> default. I included the limits.h header to use PTHREAD_STACK_MIN as
> the minimum stack size.
>
> As for the deadlock, if the pthread_create failed, the new thread
> doesn't exist and doesn't call threadStartedBlock.release(), so the
> existing thread deadlocks on threadStartedBlock.block(). Only block
> if the thread was started.
>
> --
> David Fries <david at fries.net>
> http://fries.net/~david/ (PGP encryption key available)
>
>
> Index: src/OpenThreads/pthreads/PThread.c++
> ===================================================================
> --- src/OpenThreads/pthreads/PThread.c++ (revision 10273)
> +++ src/OpenThreads/pthreads/PThread.c++ (working copy)
> @@ -21,6 +21,7 @@
> #include <sys/types.h>
> #include <unistd.h>
> #include <pthread.h>
> +#include <limits.h>
>
> #if defined __linux || defined __sun || defined __APPLE__
> #include <string.h>
> @@ -588,30 +589,29 @@
>
> PThreadPrivateData *pd = static_cast<PThreadPrivateData *> (_prvData);
>
> - size_t defaultStackSize;
> - pthread_attr_getstacksize( &thread_attr, &defaultStackSize);
> - if(status != 0) {
> - return status;
> + //-------------------------------------------------------------------------
> + // Set the stack size if requested, but not less than a platform reasonable
> + // value.
> + //
> + if(pd->stackSize) {
> + if(pd->stackSize < PTHREAD_STACK_MIN)
> + pd->stackSize = PTHREAD_STACK_MIN;
> + pthread_attr_setstacksize( &thread_attr, pd->stackSize);
> + if(status != 0) {
> + return status;
> + }
> }
>
> - if(defaultStackSize < pd->stackSize) {
> -
> - pthread_attr_setstacksize( &thread_attr, pd->stackSize);
> - if(status != 0) {
> - return status;
> - }
> - }
> -
> //-------------------------------------------------------------------------
> // Now get what we actually have...
> //
> - pthread_attr_getstacksize( &thread_attr, &defaultStackSize);
> + size_t size;
> + pthread_attr_getstacksize( &thread_attr, &size);
> if(status != 0) {
> return status;
> }
> + pd->stackSize = size;
>
> - pd->stackSize = defaultStackSize;
> -
> //-------------------------------------------------------------------------
> // Prohibit the stack size from being changed.
> //
> @@ -635,18 +635,15 @@
> status = pthread_create(&(pd->tid), &thread_attr,
> ThreadPrivateActions::StartThread,
> static_cast<void *>(this));
> -
> - // wait till the thread has actually started.
> - pd->threadStartedBlock.block();
>
> - if(status != 0) {
> - return status;
> + if(status == 0) {
> + // wait till the thread has actually started.
> + pd->threadStartedBlock.block();
> +
> + pd->idSet = true;
> }
>
> - pd->idSet = true;
> -
> - return 0;
> -
> + return status;
> }
>
> //-----------------------------------------------------------------------------
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
More information about the osg-users
mailing list