dispatchqueue.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. */
  9. #ifndef dispatchqueue_hpp
  10. #define dispatchqueue_hpp
  11. #include <thread>
  12. #include <mutex>
  13. #include <condition_variable>
  14. #include <queue>
  15. #include <functional>
  16. #include <string>
  17. class DispatchQueue {
  18. typedef std::function<void(void)> fp_t;
  19. public:
  20. DispatchQueue(std::string name, size_t threadCount = 1);
  21. ~DispatchQueue();
  22. // dispatch and copy
  23. void dispatch(const fp_t& op);
  24. // dispatch and move
  25. void dispatch(fp_t&& op);
  26. void removePending();
  27. // Deleted operations
  28. DispatchQueue(const DispatchQueue& rhs) = delete;
  29. DispatchQueue& operator=(const DispatchQueue& rhs) = delete;
  30. DispatchQueue(DispatchQueue&& rhs) = delete;
  31. DispatchQueue& operator=(DispatchQueue&& rhs) = delete;
  32. private:
  33. std::string name;
  34. std::mutex lockMutex;
  35. std::vector<std::thread> threads;
  36. std::queue<fp_t> queue;
  37. std::condition_variable condition;
  38. bool quit = false;
  39. void dispatchThreadHandler(void);
  40. };
  41. #endif /* dispatchqueue_hpp */