Multi-threading using the IThread class

Written by Gordon Zeglinski

Introduction
This issue's OOPS Avenue is in response to a reader's email. Things are extremely hectic, so sometimes email doesn't get answered as promptly as I would like it to be. Keep the mail coming, but please be patient; I'll answer it as soon as I can.

In this issue, we will look at multi-threading using the IThread class. The IThread class is part of the ICLUI shipped with C-Set++ 2.0 and 2.1; however, we will be building upon the drag and drop example of last issue, which requires C-Set++ 2.1.

The IThread Class
The IThread class is used to manipulate threads in the ICLUI. An instance of IThread and the thread it represents are not bound together. If the thread terminates, the instance is not destroyed. If the instance is destroyed, the thread continues executing.

Starting a Thread with IThread

There are two methods that can be used to start a thread. The first, is to use the one of the constructors that take a function address or a reference to an instance of IThreadFn as an argument. The second is to use the "start member" function. We will look at both methods here.

The following code snippet illustrates both methods of starting a thread. In this example, we use a member function of the class foo as the threads starting function. The class IThreadMemberFn holds both an instance of a class and a pointer to a member function of that class. These two pieces of information are need to start the thread using the correct member function and instance. Note, that since IThreadMemberFn is a template class, it can be used with any class.

In this example, we only looked at starting threads on member functions. The method of starting threads on non-member functions is almost identical to the one outlined above. Thus, it is left up to the reader to explore this further.

Building a Simple Test Application
This section should look familiar to those who have read the last issue. We will take our application from last issue, and move the file loading routine into a separate thread. In this section, we will look at only the modifications made to last issues code that are needed to accomplish our goal here.

The class definition for MyFrame has been expanded to allow multi-threaded loading of files into the MLE. Added to the version of MyFrame shown above, are several several new member functions that handle the multi-threaded file loading. The constructor is modified as follows to initialize the new data members. The targetDrop routine is modified so that it uses the member functions we added to MyFrame to set the file name and start the thread which will read in the file. In the following code, we use the fact that the parent of the MLE is an instance of MyFrame to get the instance of the MyFrame window and call the thread creation function. Now that we've seen how the old code was modified, we look at the two new member functions of MyFrame. These functions don't do anything that we haven't already seen. The source code to this issue is packaged the same way as the source code in the previous issue.

Summary
In this issue, we have seen how to use IThread and its supporting classes to create a multi-threaded PM application by building upon our drag and drop example from last issue.