Advertisement
fcamuso

C++ 20 - lezione 15

Jan 29th, 2023
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. //VERSIONE CON THREAD
  2. #include <iostream>
  3. #include <thread>
  4.  
  5. using namespace std;
  6.  
  7. void ordina_partizione_array(int *v, int quanti)
  8. {
  9.   for (int i=0; i<quanti-1; i++)
  10.     for (int j=i+1; j<quanti; j++)
  11.       if (v[j]<v[i]) swap(v[i], v[j]);
  12. }
  13.  
  14. void setup(int v[], int quanti)
  15. {
  16.   for (int i=0; i<quanti; i++)
  17.     v[i] = rand();
  18. }
  19.  
  20. void stampa(int v[], int quanti)
  21. {
  22.   for (int i=0; i<quanti; i++)
  23.     cout << v[i] << " ";
  24. }
  25.  
  26. const int MAX=30;
  27. int v[MAX];
  28.  
  29. int main()
  30. {
  31.   setup(v, MAX);
  32.  
  33.  
  34.   thread t1 = thread(ordina_partizione_array,v, MAX/2);
  35.   thread t2 = thread(ordina_partizione_array,v+MAX/2, MAX/2);
  36.  
  37.   t1.join();
  38.   t2.join();
  39.  
  40.  
  41.   stampa(v, MAX);
  42.  
  43.  
  44.     return 0;
  45. }
  46.  
  47.  
  48. //VERSIONE CON COROUTINE
  49. #include <concepts>
  50. #include <coroutine>
  51. #include <exception>
  52. #include <iostream>
  53. #include <thread>
  54. #include <algorithm>
  55. #include <format>
  56.  
  57. using namespace std;
  58.  
  59. //static int ctr{ 0 };
  60.  
  61. struct ReturnObject {
  62.     struct promise_type {
  63.         ReturnObject get_return_object() {
  64.            
  65.             return { std::coroutine_handle<promise_type>::from_promise(*this) };
  66.         }
  67.  
  68.         void return_void() noexcept {}
  69.  
  70.  
  71.         std::suspend_never initial_suspend() { return {}; }
  72.         std::suspend_never final_suspend() noexcept { return {}; }
  73.         void unhandled_exception() {}
  74.  
  75.         auto getHandle() {
  76.             return std::coroutine_handle<promise_type>::from_promise(*this);
  77.         }
  78.     };
  79.  
  80.     std::coroutine_handle<> h_;
  81.  
  82.     ReturnObject(std::coroutine_handle<> h) :h_{ h } { }
  83.  
  84.     //implicit conversion operator
  85.     operator std::coroutine_handle<>() const { return h_; }
  86. };
  87.  
  88.  
  89. //coroutine
  90. ReturnObject ordina_partizione_array(int* v, int quanti, bool& terminato, string corID) {
  91.     int progresso = 0;
  92.     cout << "inizio coroutine " + corID << endl;
  93.  
  94.     for (int i = 0; i < quanti - 1; i++)
  95.         for (int j = i + 1; j < quanti; j++)
  96.         {
  97.             if (v[j] < v[i]) std::swap(v[i], v[j]);
  98.            
  99.             progresso++;
  100.             cout << progresso << endl;
  101.  
  102.             if (progresso % 3 == 0)
  103.             {
  104.                 cout << format("{} Restituisce controllo al main ... i:{} j:{}\n",corID, i,j);
  105.                 co_await std::suspend_always{};
  106.                 cout << format("Rientro nella {} con i:{} j:{}\n", corID, i, j);
  107.             }
  108.         }
  109.  
  110.     terminato = true;
  111. }
  112.  
  113.  
  114. void setup(int v[], int quanti) { for (int i = 0; i < quanti; i++) v[i] = rand(); }
  115.  
  116. void stampa(int v[], int quanti)
  117. {
  118.     for (int i = 0; i < quanti; i++)    cout << v[i] << " ";
  119.     cout << endl;
  120. }
  121.  
  122.  
  123. const int MAX = 10;
  124. int v[MAX];
  125.  
  126. void elaborazione_main()
  127. {
  128.     for (int i = 0; i < 5; i++) cout << "Progresso workload del main ...\n";
  129. }
  130.  
  131. int main() {
  132.     setup(v, MAX);
  133.     stampa(v, MAX);
  134.  
  135.  
  136.     bool terminata_cor1 = false, terminata_cor2 = false;
  137.  
  138.     //il main lavora un po' ...
  139.     elaborazione_main();
  140.  
  141.     std::coroutine_handle<> h1 =
  142.         ordina_partizione_array(v, MAX / 2, terminata_cor1, "cor1");
  143.    
  144.     //il main lavora un po' ...
  145.     elaborazione_main();
  146.  
  147.     std::coroutine_handle<> h2
  148.         = ordina_partizione_array(v + MAX / 2, MAX / 2, terminata_cor2, "cor2");
  149.  
  150.  
  151.  
  152.     while (!terminata_cor1 && !terminata_cor2)
  153.     {
  154.         //il main lavora un po' ...
  155.         elaborazione_main();
  156.  
  157.         //passo il controllo alla coroutine che fa avanzare
  158.         //l'ordinamento per la prima metà dell'array
  159.         h1();
  160.  
  161.         //la coroutine ha restituito il controllo al chiamante che
  162.         //può avanzare un poco a sua volta
  163.         elaborazione_main();
  164.  
  165.         //passo il controllo alla coroutine che fa avanzare
  166.         //l'ordinamento per la seconda metà dell'array
  167.         h2();
  168.     }
  169.  
  170.     stampa(v, MAX);
  171.    
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement