dispatchqueue.hpp 1.7 KB

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