Thread: 1.0 internals
View Single Post
Old 07-22-2005, 06:22 AM  
darkone
Disabled
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

Quote:
Originally Posted by FTPServerTools
I assume some of those are single threads and others can be multiple available. Otherwise the threadswitching slows stuff down. And yes you only have 1088 tls spaces.
Thread counts of different types of thread pools were listed, but generally there is more than one thread of each type. Thread switching does indeed cost something (a lot of CPU time), which is why thread pools are used in the first place.


Let's compare:

With thread per client model X clients performing example task simultanously would use resources for following:
- X CreateThread()s
- More threads equals to higher chance for cache miss.
- Thread specific memory pooling does not make sense, memory allocations are costly
- OS thread switches between X active threads
- X ExitThread()s

With thread pool of size Y and client count of X:
- No CreateThread()s
- Thread specific memory pools, memory allocation may come for free.
- Manual thread switches between tasks 4 * X
- OS thread switches between Y active threads
- No ExitThread()s

Each time new thread is created, existing threads perform less work during one quantum. And if thread can't perform it's task during one quantum, OS thread switch occurs.

If task takes exactly one quantum to execute from single thread, X threads executing same task simultanously on single processor would require each X quantums - X * X thread switches.

- X * X thread switches to execute X tasks, one in each thread

When executed in thread pool size of Y, X tasks divided in 4parts would require Y * Y + (4 * X) thread switches. If thread pool size is greater than number of simultanous tasks, it takes X * X + (4 * X) thread switches to execute task.

- (2 * 2) + (4 * X) thread switches to execute X tasks divided in 4 parts, in thread pool of 2threads.

Comparison:
X^2 > 4 + 4X
X^2 - 4X > 4

Conclusion:
Thread pool size of 2 threads uses less resources when there are 5 or more simultanous tasks executing. Note that benefites of memory and other thread specific pools, greater chance for cache hits, and resources saved by not creating/deleting threads are not even taken in account.
darkone is offline   Reply With Quote