dispatchqueue.hpp 1.6 KB

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