[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Limit on maximum # of threads in Linux?



Hi!

On Wed Jul 02, 2003 at 11:31:32AM +0200, Holger Rauch wrote:
> I tried the following test program with various 2.4.x und 2.2.x kernels and
> noticed that it doesn't *seem* to be possible to create more than 1021
> threads. I changed "ulimit -u" from within bash before running the thread
> test program, I modified a setting in the /proc filesystem:
> 
> /proc/sys/kernel/threads-max
> 
> (but that limit is 12287 with a 2.4.21 kernel, so it should be high enough
> anyway),

If you don't have enough memory you can't create more threads.

[...]
> 	for ( i = 0; i < numthreads; i++ )  {
> 		(void) printf( "Thread #%d: ", (i + 1) );
> 		if ( ( retval = pthread_create( &thread_id[i], NULL, thread_func, NULL ) ) != 0 )  {
> 			(void) fprintf( stderr,
> 					"*** Unable to create thread #%d ***\n",
> 					i );
> 		}
> 		(void) printf( "thread done\n" );
> 		(void) sleep( sleepsecs );
> 	}
> 
> 	return( EXIT_SUCCESS );
> }

You forgot to add pthread_join to deallocate the memory of the threads.
Add following lines after pthread_create():

if (pthread_join (thread_id[i], NULL))
	{
		perror ("pthread_join");
		exit (1);
	}

pthread_join(3) says:

When  a  joinable  thread  terminates,  its  memory  resources  (thread
descriptor and stack) are not deallocated until another thread performs
pthread_join on it. Therefore, pthread_join must be called once for
each joinable thread created to avoid memory leaks.

So long
Thomas

-- 
 .''`.  Obviously we do not want to leave zombies around. - W. R. Stevens
: :'  : Thomas Krennwallner <djmaecki at ull dot at>
`. `'`  1024D/67A1DA7B 9484 D99D 2E1E 4E02 5446  DAD9 FF58 4E59 67A1 DA7B
  `-    http://bigfish.ull.at/~djmaecki/



Reply to: