//----------------------------------------------------- // spinlock.cc: stallings + interruptor(con spinlock) //----------------------------------------------------- #include "spinlock.h" #include #include #include #include #include //----------------------------------------------------- using namespace std::literals; //----------------------------------------------------- const size_t N = 24; // número de hebras //----------------------------------------------------- std::latch latch(N); std::mutex no_escritor; // exclusion mutua interruptor 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 / 2; ++i) { std::thread(lector, '0' + i).detach(); std::thread(escritor, 'a' + i).detach(); } std::this_thread::sleep_for(12ms); } //-----------------------------------------------------