ThreadPool.hpp
1 #pragma once
2 #include <QAbstractEventDispatcher>
3 #include <QThread>
4 
5 #include <blockingconcurrentqueue.h>
6 #include <score_lib_base_export.h>
7 #include <smallfun.hpp>
8 
9 #include <memory>
10 #include <thread>
11 namespace score
12 {
13 class SCORE_LIB_BASE_EXPORT ThreadPool
14 {
15 public:
16  ThreadPool();
17  ~ThreadPool();
18 
19  static ThreadPool& instance();
20 
21  QThread* acquireThread();
22  void releaseThread();
23 
24 private:
25  std::unique_ptr<QThread[]> m_threads;
26  int m_numThreads{};
27  int m_currentThread{};
28 
29  int m_inFlight = 0;
30 };
31 
32 class SCORE_LIB_BASE_EXPORT TaskPool
33 {
34 public:
35  TaskPool();
36  ~TaskPool();
37  static TaskPool& instance();
38 
39  template <typename F>
40  void post(F&& func)
41  {
42  m_queue.enqueue(std::forward<F>(func));
43  }
44 
45 private:
46  using task = smallfun::function<
47  void(),
48 #if defined(_MSC_VER) && !defined(NDEBUG)
49  256,
50 #else
51  128,
52 #endif
53  std::max((int)8, (int)std::max(alignof(std::function<void()>), alignof(double))),
54  smallfun::Methods::Move>;
55  moodycamel::BlockingConcurrentQueue<task> m_queue;
56  std::array<std::thread, 1> m_threads;
57  std::atomic_bool m_running{};
58 };
59 }
Definition: ThreadPool.hpp:33
Definition: ThreadPool.hpp:14
Base toolkit upon which the software is built.
Definition: Application.cpp:90