Threads and Classes

Source: comp.lang.c++.moderated
Date: 11-Sep-97

Related Sites


------------------------------

o-< Christopher Helck asked:

I'm looking for some guidelines on using threads and classes. My current design confuses me so much that it must be wrong. I have a class Foo

class Foo {
public:
   Foo();
   Run();        // Loop that does useful stuff until Stop() is called
   Stop();       // Stops the above loop
   Control();    // Change the behavior of the loop
private:
   // Ton of state variables
};
In my main() I create an object Foo and then launch a thread which runs Foo::Run(). I use Foo's Control methods to change Foo which changes the behavior of the thread.

The above works, but it seems wrong. In my code I have two classes like Foo which need to talk to each other -- it gets very messy.

I think the problem is that I have one object doing two different things: providing a service (the thread running an endless loop) and providing an interface to the service.

Does anyone have any suggestions?


---------------

o-< Robert C. Martin replied:

Threads and objects are orthogonal concepts. A thread may run in many different objects, and an object can have many different threads running through it. Thus, the association between a thread and an object is very weak. (Except in heavyweight processes where threads cannot directly share data.)

It is common to create a class called Thread that has a pure virtual Run function and some other control functions that allow a thread to be stopped, paused, or its priority changed.

class Thread
{
  public:
    virtual void Run() = 0;
    void Stop();
    void Pause();
    void Continue();
    void SetPriority(int);
};
The idea is that you create a service class that is abstract and does not know about Thread. This class has pure virtual functions that represent all its interfaces.

Then you multiply derive from your service class and Thread to create a threaded service. This class implements all the services, and the main Run loop.

A more detailed explanation of this technique can be found in chapter 4 of my book Designing Object Oriented C++ Applications using the Booch Method.

Another very good source for this kind of information is Doug Lea's Java book on Concurrent Programming. [see More Info -YS]


---------------

o-< Kevin J. Hopps added:

The function Run() is invoked within the context of one thread, and the others are (or should be) invoked within the context of another thread. For me, moving the "management" function into another class, and adding a launch() function separated the concerns nicely.


------------------------------

o-< More Info:

Greg Lavender and Douglas C. Schmidt, Active Object - An Object Behavioral Pattern for Concurrent Programming

Sun's Threads page

Doug Lea, Concurrent Programming in Java (and supplemental online material).

The Adaptive Communication Environment (ACE). This free framework provides many communication and concurrency services.


------------------------------