nclude “zthread/Guard.h” #incl
Автор admin
Среда, 1 июля 2009 11:32
nclude “zthread/Guard.h” #include “zthread/Mutex.h” #include “zthread/Thread.h” #include “Display.h” class Chopstick { ZThread::Mutex lock: ZThread:Condition notTaken; bool taken: public: ChopstickО : notTaken(lock). taken(false) {} void takeO { ZThread::Guard g(lock); while(taken) notTaken.wait(): taken = true: } void dropO { ZThread::Guard g(lock): 588 Глава 11 • Многопоточное программирование taken = false; notTaken.signal (): } class Philosopher : public ZThread::Runnable { Chopstick& left: Chopstick& right; int id: int ponderFactor; ZThread::CountedPtr display; int randSleepTime() { if(ponderFactor == 0) return 0: return rand()/(RAND_MAX/ponderFactor) * 250: } void output(std::string s) { std::ostringstream os: os « *this « ” ” « s « std::endl: display->output(os); } public: Philosopher(Chopstick& 1. Chopstick& r. ZThread::CountedPtr& disp. int ident.int ponder) : left(l). right(r). id(ident). ponderFactor(ponder). display(disp) {} virtual void run() { try { while(!ZThread::Thread::interrupted()) { outputC’thinking”); ZThread::Thread::sleep(randSle
Комментарии закрыты.
Последние комментарии