dispatchqueue.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef dispatchqueue_hpp
  19. #define dispatchqueue_hpp
  20. #include <thread>
  21. #include <mutex>
  22. #include <condition_variable>
  23. #include <queue>
  24. class DispatchQueue {
  25. typedef std::function<void(void)> fp_t;
  26. public:
  27. DispatchQueue(std::string name, size_t threadCount = 1);
  28. ~DispatchQueue();
  29. // dispatch and copy
  30. void dispatch(const fp_t& op);
  31. // dispatch and move
  32. void dispatch(fp_t&& op);
  33. void removePending();
  34. // Deleted operations
  35. DispatchQueue(const DispatchQueue& rhs) = delete;
  36. DispatchQueue& operator=(const DispatchQueue& rhs) = delete;
  37. DispatchQueue(DispatchQueue&& rhs) = delete;
  38. DispatchQueue& operator=(DispatchQueue&& rhs) = delete;
  39. private:
  40. std::string name;
  41. std::mutex lockMutex;
  42. std::vector<std::thread> threads;
  43. std::queue<fp_t> queue;
  44. std::condition_variable condition;
  45. bool quit = false;
  46. void dispatchThreadHandler(void);
  47. };
  48. #endif /* dispatchqueue_hpp */