Thread Manager

The project will not be able to use things which require application window handles ( e.g. CWinThread type of things ) as these will not be available in Console Apps ( CGI's are included in this ).

The main purpose of this project is to minimise the amount of memory and handles a program uses during its lifetime, thus giving the operating system an easy time.

In order to do this a process must be created which will watch for the thread handles becoming redundant and then calling CloseHandle upon them. This means that the DLL will actually create a separate thread which will perform some work and at some time in the future will exit and not close its handle. This can be catered for in two ways, 1) Do nothing and let the OS tidy up this one stray, or 2) expose a function to the owner of the Dll which will allow the thread to be closed gracefully and close the handle, thus releasing all memory and handles used by this extra thread. Either of the above methods can be used, but the more exceptable is the later.

Because the code will be used in an environment where it will be possible for several processes as well several threads are accessing the classes, it will be necessary to add mutex's to sections of the code when adding and deleting thread description objects.

Pseudo Code

CareTaker functionality
When the object is created :-
Initialise the AddEvent to manual mode, and,
Set the KeepGoing flag to true.
The Mutex is created with the version string.

When checking for the validity of the CareTaker object check for :-
If the object pointer is NULL, and,
	If the Object has a valid sized address space allocated.

To Add a Thread handle to the CareTakers list :-
Create a new handle definition object, and,
Create a thread access lock,
place on the end of a list,
Release the locking mechanism,
Pulse the event to check its status of the other handles.

When checking for Closure of a thread handle :-
Create a thread access locking mechanism
For each Handle definition in the CareTakers list,
	Check the Exit code status for a valid closure code,
		If the Code is not still active then,
			Remove the Handle object from the list
			Call CloseHandle on the Thread handle,
			Delete the memory allocated to the Thread Handle definition object.
Unlock the thread access lock.

The CareTaker Process :-
The first thing the process for the CareTaker does is to lower the priority of the thread that it owns to perform the tidying up.
It then creates the actual CareTaker object to perform the list holding.
While the flag to KeepRunning is true the AddEvent is pulsed every 500 MilliSecs to CloseThreadHandles.
Thread Manager Class
To create a ThreadManager
Pass the start address of routine that begins execution of new thread, [ this would have been declared as UINT APIPRIVATE A_Thread_Function( PVOID pvThreadData ), and a LPVOID pParam ( cannot be NULL) ]
Create a CareTaker if it is not valid.
Create a the processing thread for the function passed in a suspended state, and,
add the thread handle to this new ThreadManager object for external use
Add the new Thread handle to the CareTaker.
It things failed for t he CareTaker then throw an assert.

How to start the ThreadManager
Call ::ResumeThread to make the thread for the function passed perform some work.

Functional Flow

This will be done from the viewpoint of a test program to demonstrate the functionality of the code.

Download project 46K

Posted : March 12, 98



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 18, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT While the idea of using facial and or gesture recognitions to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development. There are many different ways in which Intel and Unity Technologies have been working together to helps speed the develop of games with the Intel® RealSense™ SDK (Software Developer Kit), so come hear from a panel of experts on what we've done …

  • On-demand Event Event Date: March 19, 2015 The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private app stores to …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date