讨论:一个进程(Process)最多可以生成多少个线程(Thread)

1.进程中创建线程的限制

默认情况下,一个线程的栈要预留1M的内存空间,而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程,但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小

  1. #include "stdafx.h"  
  2. #include <windows.h>  
  3. #include <process.h>  
  4. #include <assert.h>  
  5. volatile bool gbExitThread = false;  
  6. HANDLE ghDataEvent = CreateEvent(NULL, FALSE, FALSE, NULL);  
  7. UINT WINAPI SubThread(void* ptr)  
  8.     int nThreadID = *((int*)ptr);  
  9.     nThreadID++;  
  10.     SetEvent(ghDataEvent);  
  11.     printf("%d线程启动/n", nThreadID);  
  12.     int i = 0;  
  13.     while(!gbExitThread)  
  14.         Sleep(10000);  
  15.     return 0;  
  16. int _tmain(int argc, _TCHAR* argv[])  
  17.     int nThreadCount = 10000;  
  18.     HANDLE* phaThread = new HANDLE[nThreadCount];  
  19.     int nErr = 0;  
  20.     for(int i = 0; i < nThreadCount; i++)  
  21.         phaThread[i] = (HANDLE)_beginthreadex(NULL, 0, SubThread, &i, 0, NULL);  
  22.         if(phaThread[i] == 0)  
  23.             nErr = GetLastError();  
  24.             if(nErr == 8)  
  25.                 printf("开启线程失败,存储空间不足!/n");  
  26.                 printf("开启线程失败,错误号%d/n", nErr);  
  27.             break;  
  28.         WaitForSingleObject(ghDataEvent, INFINITE);  
  29.     return 0;  

2.如何突破2000个限制?    你也可以通过连接时修改默认栈大小,将其改的比较小,这样就可以多开一些线程 如将默认栈的大小改成512K,这样理论上最多就可以开4096个线程
    即使物理内存再大,一个进程中可以起的线程总要受到2GB这个内存空间的限制。比方说你的机器装了64GB物理内存,但每个进程的内存空间还是4GB,其中用户态可用的还是2GB

    如果是同一台机器内的话,能起多少线程也是受内存限制的。每个线程对象都要站用非页面内存,而非页面内存也是有限的,当非页面内存被耗尽时,也就无法创建线程了

    如果物理内存非常大,同一台机器内可以跑的线程数目的限制值会越来越大  

MSDN原文:

“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. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.

可以通过修改CreateThread参数来缩小线程栈StackSize,例如

  1. #include "stdafx.h"  
  2. #include <windows.h>  
  3. #include <process.h>  
  4. #include <assert.h>  
  5. volatile bool gbExitThread = false;  
  6. HANDLE ghDataEvent = CreateEvent(NULL, FALSE, FALSE, NULL);  
  7. UINT WINAPI SubThread(void* ptr)  
  8.     int nThreadID = *((int*)ptr);  
  9.     nThreadID++;  
  10.     SetEvent(ghDataEvent);  
  11.     printf("%d线程启动/n", nThreadID);  
  12.     int i = 0;  
  13.     while(!gbExitThread)  
  14.         printf("%d: %d/n", nThreadID, i++);  
  15.         Sleep(10000);  
  16.     printf("%d线程退出/n", nThreadID);  
  17.     return 0;  
  18. int _tmain(int argc, _TCHAR* argv[])  
  19.     int nThreadCount = 10000;  
  20.     HANDLE* phaThread = new HANDLE[nThreadCount];  
  21.     int nErr = 0;  
  22.     for(int i = 0; i < nThreadCount; i++)  
  23.         phaThread[i] = (HANDLE)_beginthreadex(NULL, 512*1024, SubThread, &i, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL);  
  24.         if(phaThread[i] == 0)  
  25.             nErr = GetLastError();  
  26.             if(nErr == 8)  
  27.                 printf("开启线程失败,存储空间不足!/n");  
  28.                 printf("开启线程失败,错误号%d/n", nErr);  
  29.             break;  
  30.         WaitForSingleObject(ghDataEvent, INFINITE);  
  31.     return 0;  

服务器端程序设计

如果你的服务器端程序设计成:来一个client连接请求则创建一个线程,那么就会存在2000个限制(在硬件内存和CPU个数一定的情况下)建议如下:

The "one thread per client" model is well-known not to scale beyond a dozen clients or so. If you're going to be handling more than that many clients simultaneously, you should move to a model where instead of dedicating a thread to a client, you instead allocate an object. (Someday I'll muse on the duality between threads and objects.) Windows provides I/O completion ports and a thread pool to help you convert from a thread-based model to a work-item-based model.

1. Serve many clients with each thread, and use nonblocking I/O and level-triggered readiness notification 
2. Serve many clients with each thread, and use nonblocking I/O and readiness change notification 
3. Serve many clients with each server thread, and use asynchronous I/O 
上面几句哈的核心的思想是:使用异步I/O ,和一个线程处理多个客户请求!!

 

此条目发表在未分类分类目录,贴了标签。将固定链接加入收藏夹。