Guest User

Untitled

a guest
Nov 15th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <chrono>
  5. #include <thread>
  6. #include <atomic>
  7. #include <mutex>
  8. #include <condition_variable>
  9.  
  10. using ClockT = std::chrono::high_resolution_clock;
  11. ClockT::time_point Start;
  12.  
  13. struct ThreadPool
  14. {
  15.     const std::size_t ThreadCount = std::max< unsigned >( 1, std::thread::hardware_concurrency() );
  16.     std::atomic< std::size_t > Running{ 0 };
  17.     bool Started = false;
  18.     bool Kill = false;
  19.     std::vector< std::thread > Threads;
  20.     std::vector< double > Measurements;
  21.  
  22.     std::condition_variable Cv;
  23.     std::mutex Mutex;
  24.    
  25.     struct Worker
  26.     {
  27.         ThreadPool* Pool;
  28.        
  29.         explicit Worker( ThreadPool* Pool )
  30.             : Pool{ Pool }
  31.         {
  32.         }
  33.        
  34.         void operator()()
  35.         {
  36.             const auto Check = [ this ]{ return this->Pool->Started || this->Pool->Kill; };
  37.  
  38.             while( true )
  39.             {
  40.                 if( !Check() )
  41.                 {
  42.                     std::unique_lock< decltype( Pool->Mutex ) > Lock( Pool->Mutex );
  43.                     Pool->Cv.wait( Lock, Check );
  44.                 }
  45.                 if( Pool->Kill )
  46.                 {
  47.                     return;
  48.                 }
  49.                 else
  50.                 {
  51.                     Pool->Measurements[ Pool->Running++ ] = std::chrono::duration_cast< std::chrono::duration< double, std::milli > >( ClockT::now() - Start ).count();
  52.                     do
  53.                     {
  54.                         // Task();
  55.                     }
  56.                     while( Pool->Started && !Pool->Kill );
  57.                     --Pool->Running;
  58.                 }
  59.             }
  60.         }
  61.     };
  62.    
  63.     ThreadPool()
  64.         : Measurements( ThreadCount )
  65.     {
  66.         Threads.reserve( ThreadCount );
  67.         while( Threads.size() < Threads.capacity() )
  68.         {
  69.             Threads.emplace_back( Worker{ this } );
  70.         }
  71.     }
  72.     ~ThreadPool()
  73.     {
  74.         Kill = true;
  75.         Cv.notify_all();       
  76.         while( !Threads.empty() )
  77.         {
  78.             Threads.back().join();
  79.             Threads.pop_back();
  80.         }
  81.     }
  82.  
  83.     void Run()
  84.     {
  85.         Started = true;
  86.         Cv.notify_all();
  87.         std::this_thread::yield();
  88.     }
  89. };
  90.  
  91. int main()
  92. {
  93.     ThreadPool Tp;
  94.     std::this_thread::sleep_for( std::chrono::milliseconds{ 100 } );
  95.     Start = ClockT::now();
  96.     Tp.Run();
  97.     std::this_thread::sleep_for( std::chrono::milliseconds{ 500 } );
  98.     for( auto const& i : Tp.Measurements )
  99.     {
  100.         std::cout << i << "ms\n";
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment