#include #include using namespace std; class Handler { int id; static int count; public: Handler() { id = count++; } int handle() { if (rand() % 3 == 0) { cout << id << " handles" << endl; return true; } else { cout << " " << id << " is busy" << endl; return false; } } }; int Handler::count = 1; void main( void ) { srand( time(0) ); Handler list[4]; for (int i=0; i < 6; i++) for (int j=0; true ; j = (j + 1) % 4) if (list[j].handle()) break; } #if 0 class Handler { int id; Handler* next; static int count; static Handler* first; static Handler* previous; public: Handler() { id = count++; if (first == 0) first = this; next = first; if (previous) previous->next = this; previous = this; } void handle() { if (rand() % 3 == 0) cout << id << " handles" << endl; else { cout << " " << id << " is busy" << endl; next->handle(); } } }; int Handler::count = 1; Handler* Handler::first = 0; Handler* Handler::previous = 0; void main( void ) { srand( time(0) ); Handler list[4]; for (int i=0; i < 6; i++) list[0].handle(); } #endif