dispatchqueue.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. class DispatchQueue {
  17. typedef std::function<void(void)> fp_t;
  18. public:
  19. DispatchQueue(std::string name, size_t threadCount = 1);
  20. ~DispatchQueue();
  21. // dispatch and copy
  22. void dispatch(const fp_t& op);
  23. // dispatch and move
  24. void dispatch(fp_t&& op);
  25. void removePending();
  26. // Deleted operations
  27. DispatchQueue(const DispatchQueue& rhs) = delete;
  28. DispatchQueue& operator=(const DispatchQueue& rhs) = delete;
  29. DispatchQueue(DispatchQueue&& rhs) = delete;
  30. DispatchQueue& operator=(DispatchQueue&& rhs) = delete;
  31. private:
  32. std::string name;
  33. std::mutex lockMutex;
  34. std::vector<std::thread> threads;
  35. std::queue<fp_t> queue;
  36. std::condition_variable condition;
  37. bool quit = false;
  38. void dispatchThreadHandler(void);
  39. };
  40. #endif /* dispatchqueue_hpp */