int main() { srand(time(0)); // Seed the random number generator try { cout << "Press <Return> to quit" << endl; CountedPtr<Jammer> jammer(new Jammer); CountedPtr<Butterer> butterer(new Butterer(jammer)); ThreadedExecutor executor; executor.execute(new Toaster(butterer)); executor.execute(butterer); executor.execute(jammer); cin.get(); executor.interrupt(); } catch(Synchronization_Exception& e) { cerr << e.what() << endl; } } ///:~
int main() { srand(time(0)); // Seed the random number generator cout << "Press <ENTER> to quit" << endl; CountedPtr<Count> count(new Count); vector<Entrance*> v; CountedPtr<Display> display(new Display); const int SZ = 5; try { ThreadedExecutor executor; for(int i = 0; i < SZ; i++) { Entrance* task = new Entrance(count, display, i); executor.execute(task); // Save the pointer to the task: v.push_back(task); } cin.get(); // Wait for user to press <Enter> count->pause(); // Causes tasks to stop counting int sum = 0; vector<Entrance*>::iterator it = v.begin(); while(it != v.end()) { sum += (*it)->getValue(); ++it; } ostringstream os; os << "Total: " << count->value() << endl << "Sum of Entrances: " << sum << endl; display->output(os); count->cancel(); // Causes threads to quit } catch(Synchronization_Exception& e) { cerr << e.what() << endl; } } ///:~
int main() { try { ThreadedExecutor executor; for(int i = 0; i < 5; i++) executor.execute(new SleepingTask(i)); } catch(Synchronization_Exception &e) { cerr << e.what() << endl; } }
int main() { cout << "Press <Enter> to quit" << endl; try { CountedPtr<ThreadLocalVariables> tlv(new ThreadLocalVariables); const int SZ = 5; ThreadedExecutor executor; for(int i = 0; i < SZ; i ++) executor.execute(new Accessor(tlv, i)); cin.get(); tlv->cancel(); } catch(Synchronization_Exception &e) { cerr << e.what() << endl; } }
int main(int argc, char* argv[]) { srand(time(0)); // Seed the random number generator int ponder = argc > 1 ? atoi(argv[1]) : 5; cout << "Press <ENTER> to quit" << endl; enum { SZ = 5 }; try { CountedPtr<Display> d(new Display); ThreadedExecutor executor; Chopstick c[SZ]; for(int i = 0; i < SZ; i++) { executor.execute( new Philosopher(c[i], c[(i+1) % SZ], d, i,ponder)); } cin.get(); executor.interrupt(); executor.wait(); } catch(Synchronization_Exception& e) { cerr << e.what() << endl; } } ///:~