D
Don
Hi,
I'm trying to test out creating large numbers of threads
under the linux 2.6 kernel. I've written some code .. see below.
For some reason, I can't get to beyond about 4090 threads, even
when I set the per-thread stack size to be fairly small.
Am I bumping into a hard limit? Or, am I missing a configuration
somewhere that would allow me to get up to 10's of thousands
of threads?
The code I'm using is below. Any assistance would be appreciated,
Thanks,
Don
TestThread.H
==============
#ifndef TEST_THREAD_H
#define TEST_THREAD_H
#include <stdio.h>
#include <pthread.h>
class TestThread
{
public:
TestThread() {if (_mutex == NULL )
{
_mutex = new pthread_mutex_t;
pthread_mutex_init(_mutex,0);
}
}
~TestThread() {}
void start(int input_parameter);
static void *entryPoint(void *pthis);
void *run();
private:
int _input_parameter;
pthread_t _thread;
static pthread_mutex_t* _mutex;
};
#endif //TEST_THREAD_H
TestThread.C
================
#include "TestThread.H"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int STACKSIZE;
pthread_mutex_t* TestThread::_mutex = NULL;
void get_thread_stack_info();
int main(int argc, char **argv)
{
if(argc > 2)
STACKSIZE = 65536*atoi(argv[2]);
else
STACKSIZE = 65536*32;
get_thread_stack_info();
for ( int i = 0; i < atoi(argv[1]); i++)
{
TestThread tt;
printf("%i\n",i);
tt.start(i);
}
sleep(10);
}
void get_thread_stack_info()
{
size_t stacksize;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr, &stacksize);
printf("default stacksize: %i\n",stacksize);
pthread_attr_setstacksize(&attr,STACKSIZE);
pthread_attr_getstacksize(&attr, &stacksize);
printf("new stacksize: %i\n",stacksize);
//printf("PTHREAD_STACK_SIZE: %i\n",PTHREAD_STACK_SIZE);
}
void *TestThread::run()
{
printf("GOT TO thread: %i\n", _input_parameter);
pthread_mutex_unlock(_mutex);
sleep(10);
return (void *)0;
}
void *TestThread::entryPoint(void *pthis)
{
//fprintf(stdout,"GOT TO entryPoint()\n");
TestThread *pt = (TestThread*)pthis;
pt->run();
return (void *)0;
}
void TestThread::start(int input_parameter)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr,STACKSIZE);
pthread_mutex_lock(_mutex);
_input_parameter = input_parameter;
int ret = pthread_create( &_thread, &attr, TestThread::entryPoint, this);
printf("ret: %i : [%s]\n",ret, strerror(ret));
}
I'm trying to test out creating large numbers of threads
under the linux 2.6 kernel. I've written some code .. see below.
For some reason, I can't get to beyond about 4090 threads, even
when I set the per-thread stack size to be fairly small.
Am I bumping into a hard limit? Or, am I missing a configuration
somewhere that would allow me to get up to 10's of thousands
of threads?
The code I'm using is below. Any assistance would be appreciated,
Thanks,
Don
TestThread.H
==============
#ifndef TEST_THREAD_H
#define TEST_THREAD_H
#include <stdio.h>
#include <pthread.h>
class TestThread
{
public:
TestThread() {if (_mutex == NULL )
{
_mutex = new pthread_mutex_t;
pthread_mutex_init(_mutex,0);
}
}
~TestThread() {}
void start(int input_parameter);
static void *entryPoint(void *pthis);
void *run();
private:
int _input_parameter;
pthread_t _thread;
static pthread_mutex_t* _mutex;
};
#endif //TEST_THREAD_H
TestThread.C
================
#include "TestThread.H"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int STACKSIZE;
pthread_mutex_t* TestThread::_mutex = NULL;
void get_thread_stack_info();
int main(int argc, char **argv)
{
if(argc > 2)
STACKSIZE = 65536*atoi(argv[2]);
else
STACKSIZE = 65536*32;
get_thread_stack_info();
for ( int i = 0; i < atoi(argv[1]); i++)
{
TestThread tt;
printf("%i\n",i);
tt.start(i);
}
sleep(10);
}
void get_thread_stack_info()
{
size_t stacksize;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr, &stacksize);
printf("default stacksize: %i\n",stacksize);
pthread_attr_setstacksize(&attr,STACKSIZE);
pthread_attr_getstacksize(&attr, &stacksize);
printf("new stacksize: %i\n",stacksize);
//printf("PTHREAD_STACK_SIZE: %i\n",PTHREAD_STACK_SIZE);
}
void *TestThread::run()
{
printf("GOT TO thread: %i\n", _input_parameter);
pthread_mutex_unlock(_mutex);
sleep(10);
return (void *)0;
}
void *TestThread::entryPoint(void *pthis)
{
//fprintf(stdout,"GOT TO entryPoint()\n");
TestThread *pt = (TestThread*)pthis;
pt->run();
return (void *)0;
}
void TestThread::start(int input_parameter)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr,STACKSIZE);
pthread_mutex_lock(_mutex);
_input_parameter = input_parameter;
int ret = pthread_create( &_thread, &attr, TestThread::entryPoint, this);
printf("ret: %i : [%s]\n",ret, strerror(ret));
}