Executor.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ALIBABACLOUD_CORE_EXECUTOR_H_
  17. #define ALIBABACLOUD_CORE_EXECUTOR_H_
  18. #include <atomic>
  19. #include <condition_variable>
  20. #include <queue>
  21. #include <vector>
  22. #include <thread>
  23. #include <mutex>
  24. namespace AlibabaCloud
  25. {
  26. class Runnable;
  27. class Executor
  28. {
  29. public:
  30. Executor();
  31. ~Executor();
  32. static Executor * instance();
  33. void execute(Runnable* task);
  34. bool isShutdown()const;
  35. bool start();
  36. void shutdown();
  37. void wakeUp();
  38. private:
  39. static Executor *self_;
  40. std::atomic<bool> shutdown_;
  41. std::queue<Runnable*> tasksQueue_;
  42. std::mutex tasksQueueMutex_;
  43. std::thread thread_;
  44. std::condition_variable cv_;
  45. std::mutex cvMutex_;
  46. };
  47. }
  48. #endif // !ALIBABACLOUD_CORE_EXECUTOR_H_