Advertisement
Guest User

sample.cpp

a guest
May 31st, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4.  
  5. using namespace std;
  6.  
  7. void sleep_fn() {
  8.     while(true) {
  9.         this_thread::sleep_for(1000ms);
  10.     }
  11. }
  12.  
  13. constexpr int threads_count = 100;
  14.  
  15. int main()
  16. {
  17.     array<thread, threads_count> threads;
  18.     for (int i = 0; i < threads_count; i++)
  19.         threads[i] = thread(sleep_fn);
  20.  
  21.     chrono::steady_clock::time_point begin = chrono::steady_clock::now();
  22.     chrono::steady_clock::time_point end;
  23.     for (int i = 0; i < 50; i++)
  24.         end = chrono::steady_clock::now();
  25.  
  26.     cout << "Time difference = " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << "[ms]" << endl;
  27.  
  28.     for (int i = 0; i < threads_count; i++)
  29.         threads[i].detach();
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement