Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #ifndef THREAD_POOL_H
  2. #define THREAD_POOL_H
  3.  
  4. #include <vector>
  5. #include <thread>
  6. #include <queue>
  7. #include <mutex>
  8. #include <condition_variable>
  9.  
  10. namespace concurrency {
  11.     class thread_pool {
  12.         protected:
  13.             std::vector<std::thread> workers;
  14.  
  15.             int workers_count;
  16.             int active_workers;
  17.  
  18.             bool stop;
  19.  
  20.             std::mutex mutex;
  21.             std::condition_variable cond;
  22.  
  23.             typedef std::lock_guard<std::mutex> lockGuard;
  24.             typedef std::unique_lock<std::mutex> uniqueLock;
  25.  
  26.             std::queue<std::function<void()>> queue;
  27.  
  28.         public:
  29.             thread_pool(int);
  30.             ~thread_pool();
  31.  
  32.             void start_workers();
  33.  
  34.             template<class F, class ...Args>
  35.             void push(F&& f, Args&&... args);
  36.  
  37.             std::function<void()> pop();
  38.  
  39.             bool should_stop() {
  40.                 return stop;
  41.             }
  42.     };
  43. }
  44. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement