//---------------------------------------------------- // binario-eb.cc //---------------------------------------------------- #include #include #include //---------------------------------------------------- using namespace std::literals; //---------------------------------------------------- const size_t N = 8; //---------------------------------------------------- class cerrojo { public: void adquirir() { auto delay = 25us; while (!sem.try_acquire()) std::this_thread::sleep_for(delay *= 2); } void liberar() { sem.release(); } private: std::binary_semaphore sem{1}; } c; //---------------------------------------------------- void seccion_critica() { std::cout << "[" << std::this_thread::get_id() << "]: "; for (size_t i = 0; i < 10; ++i) std::cout << i; std::cout << std::endl; } //---------------------------------------------------- void hebra() { while (true) { c.adquirir(); seccion_critica(); c.liberar(); } } //---------------------------------------------------- int main() { std::jthread threads[N]; for (auto &i : threads) i = std::jthread(hebra); } //----------------------------------------------------