Читаем Thinking In C++. Volume 2: Practical Programming полностью

In ZThreads, the basic class that uses a mutex and allows task suspension is the Condition, and you can suspend a task by calling wait( ) on a Condition. When external state changes take place that might mean that a task should continue processing, you notify the task by calling signal( ), to wake up one task, or broadcast( ), to wake up all tasks that have suspended themselves on that Condition object.

There are two forms of wait( ). The first takes an argument in milliseconds that has the same meaning as in sleep( ): "pause for this period of time." The difference is that in a timed wait( ):

1.       The Mutex that is controlled by the Condition object is released during the wait( ).

2.      You can come out of the wait( ) due to a signal( ) or a broadcast( ), as well as by letting the clock run out.

The second form of wait( ) takes no arguments; this version is more commonly used. It also releases the mutex, but this wait( ) suspends the thread indefinitely until that Condition object receives a signal( ) or broadcast( ).

Typically, you use wait( ) when you’re waiting for some condition to change that is under the control of forces outside the current function. (Often, this condition will be changed by another thread.) You don’t want to idly loop while testing the condition inside your thread; this is called a "busy wait," and it’s a bad use of CPU cycles. Thus, wait( ) allows you to suspend the thread while waiting for the world to change, and only when a signal( ) or broadcast( ) occurs (suggesting that something of interest may have happened), does the thread wake up and check for changes. Therefore, wait( ) provides a way to synchronize activities between threads.

Let’s look at a simple example. WaxOMatic.cpp applies wax to a Car and polishes it using two separate processes. The polishing process cannot do its job until the application process is finished, and the application process must wait until the polishing process is finished before it can put on another coat of wax. Both WaxOn and WaxOff use the Car object, which contains a Condition that it uses to suspend a thread inside waitForWaxing( ) or waitForBuffing( ):

//: C11:WaxOMatic.cpp

// Basic thread cooperation.

//{L} ZThread

#include "zthread/Thread.h"

#include "zthread/Mutex.h"

#include "zthread/Guard.h"

#include "zthread/Condition.h"

#include "zthread/ThreadedExecutor.h"

#include

#include

using namespace ZThread;

using namespace std;


class Car {

  Mutex lock;

  Condition condition;

  bool waxOn;

public:

  Car() : condition(lock), waxOn(false) {}

  void waxed() {

    Guard g(lock);

    waxOn = true; // Ready to buff

    condition.signal();

  }

  void buffed() {

    Guard g(lock);

    waxOn = false; // Ready for another coat of wax

    condition.signal();

  }

  void waitForWaxing() {

    Guard g(lock);

    while(waxOn == false)

      condition.wait();

  }

  void waitForBuffing() {

    Guard g(lock);

    while(waxOn == true)

      condition.wait();

  }

};


class WaxOn : public Runnable {

  CountedPtr car;

public:

  WaxOn(CountedPtr& c) : car(c) {}

  void run() {

    try {

      while(!Thread::interrupted()) {

        cout << "Wax On!" << endl;

        Thread::sleep(200);

        car->waxed();

        car->waitForBuffing();

      }

    } catch(Interrupted_Exception&) { /* Exit */ }

    cout << "Ending Wax On process" << endl;

  }

};


class WaxOff : public Runnable {

  CountedPtr car;

public:

  WaxOff(CountedPtr& c) : car(c) {}

  void run() {

    try {

      while(!Thread::interrupted()) {

        car->waitForWaxing();

        cout << "Wax Off!" << endl;

        Thread::sleep(200);

        car->buffed();

      }

    } catch(Interrupted_Exception&) { /* Exit */ }

    cout << "Ending Wax Off process" << endl;

  }

};


int main() {

  cout << "Press to quit" << endl;

  try {

    CountedPtr car(new Car);

    ThreadedExecutor executor;

    executor.execute(new WaxOff(car));

    executor.execute(new WaxOn(car));

    cin.get();

    executor.interrupt();

  } catch(Synchronization_Exception& e) {

    cerr << e.what() << endl;

  }

} ///:~


In Car’s constructor, a single Mutex is wrapped in a Condition object so that it can be used to manage inter-task communication. However, the Condition object contains no information about the state of your process, so you need to manage additional information to indicate process state. Here, Car has a single bool waxOn, which indicates the state of the waxing-polishing process.

Перейти на страницу:

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

3ds Max 2008
3ds Max 2008

Одни уверены, что нет лучшего способа обучения 3ds Мах, чем прочитать хорошую книгу. Другие склоняются к тому, что эффективнее учиться у преподавателя, который показывает, что и как нужно делать. Данное издание объединяет оба подхода. Его цель – сделать освоение 3ds Мах 2008 максимально быстрым и результативным. Часто после изучения книги у читателя возникают вопросы, почему не получился тот или иной пример. Видеокурс – это гарантия, что такие вопросы не возникнут: ведь автор не только рассказывает, но и показывает, как нужно работать в 3ds Мах.В отличие от большинства интерактивных курсов, где работа в 3ds Мах иллюстрируется на кубиках-шариках, данный видеокурс полностью практический. Все приемы работы с инструментами 3ds Мах 2008 показаны на конкретных примерах, благодаря чему после просмотра курса читатель сможет самостоятельно выполнять даже сложные проекты.

Владимир Антонович Верстак , Владимир Верстак

Программирование, программы, базы данных / Программное обеспечение / Книги по IT