I am working on a C++ project in which I'm trying to create hundreds of threads using the below class. If there is a few threads, everything works fine. But if I try to create 1000 threads, only around 500 (not more than 600) threads can be created. In this situation, I would like to change the default stack size of each thread.
So, how can I change the stack size?
class CThreadPool
{
static const int MAX_THREADS = 10000;
public:
template <typename T>
static void QueueUserWorkItem(void (T::*function)(void),
T *object, ULONG flags = WT_EXECUTELONGFUNCTION)
{
typedef std::pair<void (T::*)(), T *> CallbackType;
std::auto_ptr<CallbackType> p(new CallbackType(function, object));
WT_SET_MAX_THREADPOOL_THREADS(flags, MAX_THREADS);
if (::QueueUserWorkItem(ThreadProc<T>, p.get(), flags))
{
// The ThreadProc now has the responsibility of deleting the pair.
p.release();
}
else
{
throw GetLastError();
}
}
private:
template <typename T>
static DWORD WINAPI ThreadProc(PVOID context)
{
typedef std::pair<void (T::*)(), T *> CallbackType;
std::auto_ptr<CallbackType> p(static_cast<CallbackType *>(context));
(p->second->*p->first)();
return 0;
}
};