The preferred method for compiling ZThreads for most flavors of UNIX (Linux, SunOS, Cygwin, etc.) is to use the configure script. After unpacking the files (using tar), simply execute:

./configure && make install

from the main directory of the ZThreads archive to compile and install a copy of the library in the /usr/local directory. You can customize a number of options when using this script, including the locations of files. For details, use this command:

./configure –help

The ZThreads code is structured to simplify compilation for other platforms and compilers (such as Borland, Microsoft, and Metrowerks). To do this, create a new project and add all the .cxx files in the src directory of the ZThreads archive to the list of files to be compiled. Also, be sure to include the include directory of the archive in the header search path for your project. The exact details will vary from compiler to compiler so you’ll need to be somewhat familiar with your toolset to be able to use this option.

Once the compilation has succeeded, the next step is to create a project that uses the newly compiled library. First, let the compiler know where the headers are located so that your #include statements will work properly. Typically, you will add an option such as the following to your project:

-I/path/to/installation/include

If you used the configure script, the installation path will be whatever you selected for the prefix (which defaults to /usr/local). If you used one of the project files in the build directory, the installation path would simply be the path to the main directory of the ZThreads archive.

Next, you’ll need to add an option to your project that will let the linker know where to find the library. If you used the configure script, this will look like:

-L/path/to/installation/lib –lZThread

If you used one of the project files provided, this will look like:

-L/path/to/installation/Debug ZThread.lib

Again, if you used the configure script, the installation path will be whatever you selected for the prefix. If you used a provided project file, the path will be the path to the main directory of the ZThreads archive.

Note that if you’re using Linux, or if you are using Cygwin (www.cygwin.com) under Windows, you may not need to modify your include or library path; the installation process and defaults will often take care of everything for you.

Under Linux, you will probably need to add the following to your .bashrc so that the runtime system can find the shared library file LibZThread-x.x.so.O when it executes the programs in this chapter:

export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}

(Assuming you used the default installation process and the shared library ended up in /user/local/lib; otherwise, change the path to your location).

<p>Defining Tasks</p>

A thread carries out a task, so you need a way to describe that task. The Runnable class provides a common interface to execute any arbitrary task. Here is the core of the ZThread Runnable class, which you will find in Runnable.h in the include directory, after installing the ZThread library:

class Runnable {

public:

  virtual void run() = 0;

  virtual ~Runnable() {}

};

By making this a pure abstract base class (or as pure as possible, given the constraints on virtual destructors), Runnable allows an easy mixin combination with a base class and other classes.

To define a task, simply inherit from the Runnable class and override run( ) to make the task do your bidding.

For example, the following LiftOff task displays the countdown before liftoff:

//: C11:LiftOff.h

// Demonstration of the Runnable interface.

#ifndef LIFTOFF_H

#define LIFTOFF_H

#include "zthread/Runnable.h"

#include

class LiftOff : public ZThread::Runnable {

  int countDown;

  int id;

public:

  LiftOff(int count, int ident = 0) :

    countDown(count), id(ident) {}

  ~LiftOff() {

    std::cout << id << " completed" << std::endl;

  }

  void run() {

    while(countDown--)

      std::cout << id << ":" << countDown << std::endl;

    std::cout << "Liftoff!" << std::endl;

  }

};

#endif // LIFTOFF_H ///:~

As usual, we are careful not to use any using namespace directives in a header file. The identifier id allows you to distinguish between multiple instances of the task. If you only make a single instance, you can use the default value for ident. The destructor will allow you to see that a task is properly destroyed.

In the following example, the task’s run( ) is not driven by a separate thread; it is simply called directly in main( ):

//: C11:NoThread.cpp

#include "LiftOff.h"

int main() {

  LiftOff launch(10);

  launch.run();

} ///:~

When a class inherits Runnable, it must have a run( ) function, but that’s nothing special—it doesn’t produce any innate threading abilities.

To achieve threading behavior, you must use the Thread class.

<p>Using Threads</p>
Перейти на страницу:

Похожие книги