


|
Personal page of Konjengbam |
|
“Bluetooth adopter, technology enabler and innovative thinking” |

Why Threads and When to UseQuite often when we are in the middle of developing complex programs or applications we find that multi-threading is required in order to achieve better software or application performance. Under win32 systems, a programmer or windows developer can create Threads to handle multiple operations concurrently. So, while task 'A' is being carried out by a Thread 'T-A', another long tasks may be carried out by Thread 'T-B'. The windows GUI framework runs on multi-threaded approach only. If we don't use multi-threads for long process operations, the application is blocked and end-user will not be able to perform any other activities. Sometimes, it takes so long time and your GUI application etc may be blocked also that user is forced to reboot computer even. Threads come into use in such a situation where you can achieve better software performance at the expense of creating a little extra memory overhead which is needed for creating threads within the process. Threads in Win32There are many ways in which you can create threads under Microsoft windows environment, based on framework selected chosen. Threads as such are not part of native C or C++ languages. Certain libraries and APIs are provided and a programmer makes used of those to incorporate threading in their development schemes. Under win32 systems, threads can be created using the win32 API method 'CreateThread'. In essence, one calls this function with certain parameters (see win32 API documentation) and a handle is returned once the creation is successful. This handle is passed to a thread function where long operations will be processed inside. When operation inside the thread function completes, the thread handle must be cleared and resources returned to the operation system. In this context, use is made of the API method 'CloseHandle'. About Thread ManagerThe thread manager here is an implementation that is used to manage the created threads. Worker threads may be created via the thread manager and registered to the manager to inform that this thread be kept under its watch-list. A project application requiring simple multi-threaded paradigm can make use of this thread manager systems where ordinary worker threads need not be monitored separately, but is implicitly done by the manager once you registered the thread to the manager. The manager may be turned on for starting at app start-up and shut down while app is closing. This ensures that all threads are closed first before exiting application. Implementation and DesignThe thread manager classes provide a way of operating on Win32 Threads, providing a wrapper C++ class (alternative to CWinThread of MFC) based on Win32 thread APIs. As of now, only worker threads are supported. It is advised to use CThreadManager to create worker threads. This manager monitors the threads lifetime, and when not required it flushes the threads resources. The two important classes here are CUserThread and CThreadManager. This will start a thread that will monitor other threads created using CUserThread object. See the interface definition file below. |