WINDOWS作業系統中可以允許最大的線程數

來源:互聯網
上載者:User

預設情況下,一個線程的棧要預留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-triggered readiness notification2. Serve many clients with each thread, and use nonblocking I/O and readiness change notification3. Serve many clients with each server thread, and use asynchronous I/O

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.