WINDOWS操作系统中可以允许最大的线程数

http://www.cnblogs.com/findumars/p/5597356.html


默认情况下,一个线程的栈要预留1M的内存空间 
而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 
但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小。 
你也可以通过连接时修改默认栈大小,将其改的比较小,这样就可以多开一些线程。 
如将默认栈的大小改成512K,这样理论上最多就可以开4096个线程。 

即使物理内存再大,一个进程中可以起的线程总要受到2GB这个内存空间的限制。 
比方说你的机器装了64GB物理内存,但每个进程的内存空间还是4GB,其中用户态可用的还是2GB。


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

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

在Windows下写个程序,一个进程Fork出2000个左右线程就会异常退出了,为什么?

这个问题的产生是因为windows32位系统,一个进程所能使用的最大虚拟内存为2G,而一个线程的默认线程栈StackSize为1024K(1M),这样当线程数量逼近2000时,2000*1024K=2G(大约),内存资源就相当于耗尽。

 

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,028 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.”

 

如何突破2000个限制?

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

#define   MAX_THREADS   50000
 
DWORD   WINAPI   ThreadProc(   LPVOID   lpParam   ){
while(1){
Sleep(100000);
}
return   0;
}
 
int   main()   {
DWORD   dwThreadId[MAX_THREADS];
HANDLE   hThread[MAX_THREADS];
 
for(int   i   =   0;   i   <   MAX_THREADS;   ++i)
{
hThread[i]  = CreateThread(0,  64, ThreadProc, 0, STACK_SIZE_PARAM_IS_A_RESERVATION,   &dwThreadId[i]);
 
if(0   ==   hThread[i])
{
DWORD   e   =   GetLastError();
printf("%d\r\n",e);
break;
}
}
ThreadProc(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-triggeredreadiness notification
2. Serve many clients with each thread, and use nonblocking I/O and readiness changenotification
3. Serve many clients with each server thread, and use asynchronous I/O

http://www.cnblogs.com/lancidie/archive/2011/12/15/2289337.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,操作系统是计算机系统的核心软件,负责管理和控制计算机硬件资源以及提供各种服务和功能。在2024年的考研笔记,以System Five系统为例,该系统允许多个写进程同时访问一个管道,但只允许一个读进程。这样可以确保据的一致性和避免冲突。System Five系统最初由AT&T Bell Labs在20世纪80年代开发,是UNIX操作系统的一个分支,主要用于商业领域的大型计算机和服务器。 传统的计算机在一个物理机器上只能运行一个操作系统,而在商业环境,同时运行两个应用程序可能会导致相互影响和资源竞争的问题。这也是为什么在操作系统设计需要考虑如何解决这些隐患的原因。 在实际应用操作系统可以分为不同类型,例如大内核/宏内核/单内核操作系统(如Linux、UNIX)和微内核操作系统(如Windows NT)。每种类型都有各自的特点和适用场景。 总结来说,操作系统是计算机系统的核心软件,负责管理和控制计算机硬件资源,为应用程序提供服务和功能。在2024考研笔记,可以涉及不同类型的操作系统,以及解决多进程访问资源的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [2024考研408-操作系统 第二章-进程与线程 学习笔记](https://blog.csdn.net/cl939974883/article/details/131679526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [2024考研408-操作系统 第一章-计算机系统概述学习笔记](https://blog.csdn.net/cl939974883/article/details/131543432)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值