You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

threadpool.h 685B

1 year ago
1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. Thread pool
  3. */
  4. #pragma once
  5. #include <thread>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <mutex>
  10. #include <condition_variable>
  11. namespace vtk {
  12. class ThreadPool {
  13. private:
  14. ThreadPool();
  15. ThreadPool(ThreadPool const& copy);
  16. ThreadPool& operator=(ThreadPool const& copy);
  17. public:
  18. static ThreadPool& getInstance();
  19. void addThreads(const int& count); // add new threads to the pool
  20. void addJob(std::function<void()> newJob); // add a job to the pool
  21. int threadCount();
  22. private:
  23. void threadIdle();
  24. std::vector<std::thread> mThreads;
  25. std::queue<std::function<void()> > mQueue;
  26. std::mutex mQueueMutex;
  27. std::condition_variable mCondition;
  28. };
  29. }