lock: Condition toastReady: C

Автор admin Суббота, 27 июня 2009 11:32

lock: Condition toastReady: CountedPtr jammer; bool gotToast: int buttered; public: Butterer(CountedPtrOammer>& j) : toastReady(lock). jammer(j) { gotToast = false; buttered = 0; } void moreToastReadyO { Guard g(lock): gotToast = true: toastReady.signal О; } void run() { try { while(IThread::interrupted()) { { Guard g(lock); while(!gotToast) toastReady.waitO; ++buttered; } cout « “Buttering toast ” « buttered « endl; jammer->moreButteredToastReady(); { Guard g(lock); gotToast = false; } } } catch(Interrupted_Exception&) { /* Exit */ } cout « “Butterer off” « endl; } }: class Toaster : public Runnable { CountedPtr butterer; int toasted; public: Toaster(CountedPtr& b) : butterer(b) { toasted = 0: } void run() { try { while(IThread::interrupted()) { Thread::sieep(rand()/(RAND_MAX/5)*100); // … // Изготовление нового тоста // … cout « “New toast ” « ++toasted « endl: butterer->moreToastReady(); 576 Глава 11 • Многопоточное программирование } catch(Interrupted_Exception&) { /* Выход */ } cout « “Toaster off” « endl: } }: int mainO { srand(time(0)): // Ра


Комментарии закрыты.