fork download
  1. #include <mutex>
  2. #include <vector>
  3. #include <functional>
  4.  
  5. struct dispatcher {
  6. using queue_type = std::vector<std::function<void()>>;
  7. using guard_type = std::lock_guard<std::mutex>;
  8. queue_type work;
  9. std::mutex mutex;
  10.  
  11. template<typename Functor>
  12. void push(Functor&& functor)
  13. {
  14. guard_type guard(mutex);
  15. work.emplace_back(std::forward<Functor>(functor));
  16. }
  17.  
  18. void operator()()
  19. {
  20. queue_type to_process;
  21. {
  22. guard_type guard(mutex);
  23. to_process = std::move(work);
  24. }
  25.  
  26. for(auto&& task: to_process) {
  27. task();
  28. }
  29. }
  30. };
  31.  
  32. int main()
  33. {
  34. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty