一个进程最多能开多少个线程?该如何处理

一个进程最多能开多少个线程?
最近无聊写了个小程序,在主进程中开一个线程,让这个线程不断的开线程,来测试最多能开多少线程。结果每次执行的结果都是2010(加上主线程和另外开的一个线程,一共是2012个线程)。

程序地址如下:

http://feng32.50webs.com/ThreadMark.zip

核心代码如下(C语言的):

C# code


// Create threads as many as possible
while(TRUE)
{
    hThread = CreateThread(0, 0, TestThreadProc, 0, 0, 0);
    if( hThread != NULL )
    {
        // ...
        g_arrhThreads[g_nThreads] = hThread;
        g_nThreads += 1;
    }
    else // Error occurred
    {
        // ...
        // Stop Threads
        g_bContinue = FALSE;
        WaitForMultipleObjects(g_nThreads, g_arrhThreads, TRUE, INFINITE);
        break;
    }
    Sleep(0);
}

// 线程函数如下

DWORD WINAPI TestThreadProc(LPVOID lpParam)
{
    while( g_bContinue )
    {
        Sleep(1);
    }

    return 0;
}




问题是:为什么只能开2010个线程?能想办法开更多的线程吗?

------解决方案--------------------
The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,048 threads. If you reduce the default stack size, you can create more threads.---------From MSDN 之 CreateThread Function
------解决方案--------------------
探讨
引用:

The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at m……

------解决方案--------------------
默认每线程1MB堆栈的话,只能开2048线程(如果你的其它系统资源足够的话)。要想开更多线程,只能修改每个线程的堆栈,但实际中是不推荐这样做的,因为如果你的线程因为一些工作因为线程堆栈不够的话,会导致整个进程崩溃.修改堆栈的方法好像只在XP或以上系统有效,windows 2000中不支持。