ThreadMentor: Creating and Terminating Threads

ThreadMentor has a base class Thread for thread construction in which all vital information are defined, including the hidden interface to the visualization subsystem. Thus, a thread should inherit everything from base class Thread. Moreover, the statements of a thread should be in the method ThreadFunc() of class Thread. In your thread class, you may add other variables, constructors, destructors, and so on. To create a thread, either we declare a variable of that thread class or use new to actually create one. After this, the thread is created. To run it, we have to call the method ThreadFunc() of that thread. When the execution of a thread reaches a return point or executes the call to Exit(), it terminates.

The following defines a class TestThread, derived from the base thread class Thread. We add a constructor that displays the int argument passed to it. There are two private members, one of which is the required method ThreadFunc() that will be run as a thread.

class TestThread : public Thread
{
     public:
          TestThread(int no): number(no) 
          {
               cout << "Test thread " << no << " is running\n";
          };

     private:
          int  number;
          void ThreadFunc();
};

The method ThreadFunc() contains all execution code. The first declaration line must be Thread::ThreadFunc(); so that the thread function can be properly recognized by the system. The following version is a very simple one. It simply prints out 10 lines, each of which contains the number this thread receives (by the constructor) and the value of the running index i. The call to function Exit() terminates the thread.

void TestThread::ThreadFunc()
{
     Thread::ThreadFunc();  // must be the first line
     int  i;

     for (i = 0; i < 10; i++) 
          cout << "Thread " << number << " prints " << i << "\n";
     Exit();
}

After the definition of thread TestThread completes, we can use it in our program. The following example shows how to declare and run threads. The following declares an array Running of three elements, each of which is a pointer to a thread of type TestThread. To run these threads, we create them with new and store their pointers in the corresponding element of Running. Then, we run the method Begin() of each thread to execute the thread function ThreadFunc().

void  main(void)
{
     TestThread* Running[3];
     int         i;

     for (i = 0; i < 3; i++) {
          Running[i] = new TestThread(i);
          Running[i]->Begin();
     }
     Exit();
}

In this short code segment, the main program is the parent thread and the three threads created are child threads. After the calls to Begin(), all four threads are running. If the parent thread reaches the end (i.e., the call to Exit()faster than the three child threads do, as mentioned in a previous page, all three child threads will also be terminated. On the other hand, if a child thread runs faster, after prints 10 lines, it terminates without affecting the others.

The Details

Here are the steps you need to follow in order to create, run, and terminate threads: