//----------------------------------------------------- // interruptor2.cc: stallings implementado con un interruptor //----------------------------------------------------- #include "interruptor2.h" #include #include #include #include #include //----------------------------------------------------- using namespace std::literals; //----------------------------------------------------- const size_t N = 1; //----------------------------------------------------- std::latch latch(2 * N); std::mutex no_escritor; // exclusion mutua interruptor2 i; // protección lectores frente a escritores //----------------------------------------------------- void seccion_critica(char c) { for (char i = 0; i < 10; ++i) std::cout << c; std::cout << '\n'; } //----------------------------------------------------- void lector(char c) { latch.arrive_and_wait(); while (true) { i.lock(no_escritor); seccion_critica(c); i.unlock(no_escritor); } } //----------------------------------------------------- void escritor(char c) { latch.arrive_and_wait(); while (true) { no_escritor.lock(); seccion_critica(c); no_escritor.unlock(); } } //----------------------------------------------------- int main() { for (size_t i = 0; i < N; ++i) { std::thread(lector, '0' + i).detach(); std::thread(escritor, 'a' + i).detach(); } std::this_thread::sleep_for(10ms); } //-----------------------------------------------------