A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.codeproject.com/Articles/3631/A-Method-of-Worker-Thread-Pooling below:

A Method of Worker Thread Pooling

Introduction

In many of our applications we use multi threading for better performance. Most of us already knows and using multi threading applications. And we know that thread creation is a resource intensive job. It is always better to reuse the threads, and most of us do this also. This article talks about such a thread pooling (worker thread) architecture. It will be great if you can provide let me know about any bugs that are found and any improvements that can be done to this.

Architecture in brief

The main architecture of the thread pool is given below in the class diagram.

As you can see in the above class diagram the application has mainly three classes, they are

Using the code

To use the thread pool code we have to do the following 

Additional Features How To Set Max Free Pool Size

Some times our application may need let 100 threads but most of the time the required thread is less than 20. In that case it is no need to create and keep 100 threads always in the pool. In such cases we can define the maximum free pool size to 20.This will make sure that not more than 20 idle threads will be available in the free pool. If required more number of threads will be created as on demand. Sample code is given below.  

m_pThPool = new CThPool();
m_pThPool->setMaxPoolSize(100);          
m_pThPool->setMaxFreePoolSize(20);       
Getting Statistics

Some times we may require statistics to know at any instance of time how many threads are busy and how many threads are free, what is the pool size now, etc. etc. We can get such information by calling some methods of pool. In the sample application the statistics are also used. The functions that are available to get the statistical information are  

int CThPool::getCurPoolSize()      
int CThPool::getMaxPoolSize()      
int CThPool::getMaxFreePoolSize()  
int CThPool::getFreePoolSize()     
                                   
About The Demo Program 

The demo program uses one thread pool. Initially the pool has started with one thread. And the Max pool size is 5. And the maximum free thread size is 2. In the demo program CThTargetSuper is the class, which extends CThTarget and implement the run() method. The run method simply increments the progress bar after sleeping 100 ms till it reaches the end. If more number of threads will be assigned then the progress bar will move fast. The run() methods ends when the progress bar reaches the end of the bar. And the after that the instance of the CThTargetSuper gets deleted by the thread (As the Auto Delete flag is TRUE by default) and the threads returns to the free pool and remains in suspended mode till another work get assigned. The thread may also exit if the number of threads that are present in the free pool is more than the maximum limit.

Executing the Demo Program

After you start the program first create the pool by clicking the CreatePool button. Then you can click on the AssignThread button to assign a new thread. You can not assign more threads than the Max Pool size. The current number of threads busy in the pool is displayed in the statistics along with the idle threads and max pool size. You can change the the maximum pool size by clicking the button "+" or "-" . (Upper limit is defined as 20). You can also change the maximum free pool size by clicking the corresponding "+" / "-" button. You can also press the Refresh button to refresh the statistics. The statistics also gets automatically updated around every 2 seconds.

To repeat the process click on the Reset button so that the progress bar will be reset to initial position. And you can assign the threads again to increase the progress bar's progress.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3