Advertisement
GeneralGDA

Threads count

Jan 21st, 2023 (edited)
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | Software | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <vector>
  4. #include <chrono>
  5.  
  6. int main()
  7. {
  8.     std::vector<std::thread> threads;
  9.     int counter = 0;
  10.  
  11.     while (true)
  12.     {
  13.         try
  14.         {
  15.             threads.emplace_back
  16.             (
  17.                 []
  18.                 {
  19.                     using namespace std::chrono_literals;
  20.                     std::this_thread::sleep_for(3600s)  ;
  21.                 }
  22.             );
  23.             counter++;
  24.         }
  25.         catch (...)
  26.         {
  27.             std::cout << "thread count: " << counter << std::endl;
  28.             break;
  29.         }
  30.     }
  31.  
  32.     for (auto && thread : threads)
  33.     {
  34.         thread.join();
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement