Thread Pools: Benefits of using Thread Pools

WebServers generally provide multithreading. Whenever the server receives the request, it creates a separate thread to serve a request. As The creation of thread is more reliable, feasible, and superior to create a process. Creating a separate thread for a particular request by a multithreaded server possess some problems.
> The amount of time required for creating a thread for servicing a particular request.
> The thread will be discarded after the completion of the request.
> If a multithreaded server allows all concurrent requests to be serviced in a new thread without providing any certain bound or limit on threads. Then these unbounded number of threads could exhaust system resources such as memory or CPU bound time. It will create a scenario of Denial of Service Attack.

The solution to such a problem is a thread pool. The concept of the thread pool is to create a certain number of threads at the initial level when the process starts. These threads should be placed in a pool, where they sit and wait for executing any kind of task. Whenever a server receives a request. It awakes a thread from the pool in case if any thread is available and give it to the request. Once any thread completes its service, it returns to the pool and waiting for new service requests.

If there are no available threads in the pool, then the server waits until one of the threads completes its service request.

Windows 32 bit based provides Thread Pool API. Using such a thread pool API is quite similar to creating the thread. Here, a function runs whenever a separate thread defined. Such a function is

Image 1

In a thread pool API, there is a function as QueueUserWorkItem() acquire three parameters

Image 2

LPTHREAD_START_ROUTINE function - for running a separate thread, it is a pointer to the function.
PVOID Context - parameter passed to a function
ULONG Flags - It manages the execution of the thread.

Looking to hire react js developers?

You can use CronJ Services without any fear of data breach with our strategy to hire ReactJS developers. Our simple mobile application design framework is both hybrid and native. We are the leader in Bangalore, India with a focus on Outsourced services. Hire react native app developers from CronJ which considered the best offshore offshore company for the software development ReactJS, the productive approach for web application development is discussed.

Invoking function will be like

Image 3

This causes a thread from the thread pool to invoke PoolFunc() on behalf of the programmer. Passing flag as 0 means providing the thread pool with no special instructions.

The benefit of Thread Pools:

> It is faster to service a request with the thread which is available in the pool rather than creating a new thread.
> The concept of the thread pool is limiting the number of threads at any point in time.
> Thread Pools are most important on such systems which can not support the unbounded number of request.

Conclusion: As we can see Thread Pool is the most useful way of limiting the number of threads and solving the problem of resource thrashing. Thread Pooling is basically providing the solution to the amount of physical memory used. It also limits the expected number of concurrent client requests.

この記事が気に入ったらサポートをしてみませんか?