Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <string>
  4. #include <vector>
  5. #include <mutex>
  6. #include <unistd.h>
  7.  
  8. using namespace std;
  9.  
  10. struct Barber {
  11.     int sleeping = 1;
  12.     int sleeping_time = 0;
  13.     int client_id = -1;
  14. } ;
  15.  
  16. struct Client {
  17.     int look_position = -1;
  18. } ;
  19.  
  20. //
  21. std::mutex sync_barbers_mutex;
  22. std::mutex sync_clients_mutex;
  23. std::mutex sync_queue_mutex;
  24. std::mutex log_mutext;
  25.  
  26. std::vector<Barber> barbers(4);
  27. std::vector<Client> clients(50);
  28.  
  29. int barbers_count = 0;
  30. int clients_count = 0;
  31.  
  32. std::vector<int> clients_queue(20, -1);
  33.  
  34. // IMPORTANTE! verificar se esta utilizando o mutex sync_barbers_mutex antes de chamar
  35. int check_barber() {
  36.     int barber_id = 0;
  37.     int max_sleep = -1;
  38.     int best_barber_id = -1;
  39.     for(auto &barber : barbers) {
  40.         if(!barber.sleeping) {
  41.             best_barber_id = barber_id;
  42.             break;
  43.         } else if(barber.client_id == -1 && barber.sleeping_time > max_sleep) {
  44.             max_sleep = barber.sleeping_time;
  45.             best_barber_id = barber_id;
  46.         }
  47.         barber_id++;
  48.     }
  49.     return best_barber_id;
  50. }
  51.  
  52. //
  53. void client_process() {
  54.     // Assign client
  55.     sync_clients_mutex.lock();
  56.     int id = clients_count++;
  57.     log_mutext.lock();
  58.     std::cout << "Cliente " << id << std::endl;
  59.     log_mutext.unlock();
  60.     sync_clients_mutex.unlock();
  61.  
  62.     // Reserva seu lugar caso seja possível
  63.     int clients_count = 0;
  64.     int position = -1;
  65.     sync_queue_mutex.lock();
  66.     for(int i=19; i>=0; i--) {
  67.         if(clients_queue[i] == -1) {
  68.             position = i;
  69.             continue;
  70.         }
  71.         break;
  72.     }
  73.     // Encontrou um lugar
  74.     if(position != -1) {
  75.         clients_queue[position] = id;
  76.         sync_barbers_mutex.lock();
  77.         int best_barber_id = check_barber();
  78.         sync_barbers_mutex.unlock();
  79.  
  80.         log_mutext.lock();
  81.         if(position == 0 && best_barber_id != -1)
  82.             std::cout << "Cliente " << id << " entrou na barbearia e ja vai se atendido." << std::endl;
  83.         else if(position <= 9)
  84.             std::cout << "Cliente " << id << " entrou na barbearia e esta esperando sentado na posição " << position << "." << std::endl;
  85.         else
  86.             std::cout << "Cliente " << id << " entrou na barbearia e esta esperando de pe na posição " << position << "." << std::endl;
  87.         log_mutext.unlock();
  88.         sync_queue_mutex.unlock();
  89.     // Estava lotado
  90.     } else {
  91.         log_mutext.lock();
  92.         std::cout << "Cliente " << id << " foi embora porque não tinha lugar para esperar." << std::endl;
  93.         log_mutext.unlock();
  94.         sync_queue_mutex.unlock();
  95.     }
  96.  
  97.     //
  98.     while(true) {
  99.         // Verifica se existe algum barbeiro livre (O que esta acordado ou o que esta dormindo mais)
  100.         if(position == 0) {
  101.             sync_barbers_mutex.lock();
  102.             int best_barber_id = check_barber();
  103.             // Caso tenha encontrado algum barbeiro
  104.             if(best_barber_id != -1) {
  105.                 // Retira cliente da fila
  106.                 sync_queue_mutex.lock();
  107.                 clients_queue[0] = -1;
  108.                 sync_queue_mutex.unlock();
  109.  
  110.                 // Inicia corte de cabelo
  111.                 log_mutext.lock();
  112.                 std::cout << "Cliente " << id << " iniciou o corte de cabelo." << std::endl;
  113.                 log_mutext.unlock();
  114.                 barbers[best_barber_id].client_id = id;
  115.                 //
  116.                 sync_barbers_mutex.unlock();
  117.                 //
  118.                 break;
  119.             }
  120.             sync_barbers_mutex.unlock();
  121.         } else {
  122.             sync_queue_mutex.lock();
  123.  
  124.             for(int i=position; i>=0; i--) {
  125.                 if(clients_queue[i] == -1) {
  126.                     clients_queue[id] = -1;
  127.                     clients_queue[i] = id;
  128.                     position = i;
  129.                     log_mutext.lock();
  130.                     std::cout << "Cliente " << id << " ipulou para posição " << position << "." << std::endl;
  131.                     log_mutext.unlock();
  132.                     break;
  133.                 }
  134.             }
  135.             sync_queue_mutex.unlock();
  136.         }
  137.  
  138.         //
  139.         usleep(100000);
  140.         //break;
  141.     }
  142.  
  143.     log_mutext.lock();
  144.     std::cout << "Cliente morreu" << std::endl;
  145.     log_mutext.unlock();
  146. }
  147.  
  148. int main()
  149. {
  150.     std::vector<std::thread> clients_threads;
  151.  
  152.     // Main process
  153.     for(int i=0; i<25; i++) {
  154.         clients_threads.push_back(std::thread(&client_process));
  155.         usleep(50000);
  156.         //client.join();
  157.     }
  158.  
  159.     while(clients_threads.size() > 0) {
  160.         clients_threads.back().join();
  161.         clients_threads.pop_back();
  162.     }
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement