//--------------------------------------------------------- // stallings.cc: prioridad a los lectores //--------------------------------------------------------- #include #include #include #include #include #include //--------------------------------------------------------- using namespace std; //--------------------------------------------------------- atomic run(true); // finish when false mutex em_leyendo; // exclusión mutua leyendo unsigned leyendo = 0; // nº lectores mutex no_escritor; // exclusion mutua l/e //--------------------------------------------------------- void seccion_critica(char c) { for (char i = 0; i < 10; ++i) cout << c; cout << endl; } //--------------------------------------------------------- void lector(char c) { while (run) { em_leyendo.lock(); if (++leyendo == 1) no_escritor.lock(); em_leyendo.unlock(); seccion_critica(c); em_leyendo.lock(); if (--leyendo == 0) no_escritor.unlock(); em_leyendo.unlock(); } } //--------------------------------------------------------- void escritor(char c) { while (run) { no_escritor.lock(); seccion_critica(c); no_escritor.unlock(); } } //--------------------------------------------------------- int main() { const unsigned N = 8; thread lectores[N], escritores[N]; std::default_random_engine engine; for (unsigned i = 0; i < N; ++i) if (engine() & 1) { lectores[i] = thread( lector, '0' + i); escritores[i] = thread(escritor, 'a' + i); } else { escritores[i] = thread(escritor, 'a' + i); lectores[i] = thread( lector, '0' + i); } this_thread::sleep_for(100ms); run = false; for(thread& i: lectores) i.join(); for(thread& i: escritores) i.join(); } //---------------------------------------------------------