lock_class.hpp 545 B

123456789101112131415161718192021222324252627
  1. #pragma once
  2. #include <thread>
  3. #include <mutex>
  4. template <class Type>
  5. class lockFunc {
  6. public:
  7. template<class Func, class ... Args>
  8. int lock_exe(Func func, Args&&... args) {
  9. lock.lock();
  10. bool ret = ((Type* )this->*func)(args...);
  11. lock.unlock();
  12. return ret;
  13. }
  14. template<class Func, class ... Args>
  15. int lock_exe(Func func, Type* p, Args&&... args) {
  16. lock.lock();
  17. bool ret = (p->*func)(args...);
  18. lock.unlock();
  19. return ret;
  20. }
  21. private:
  22. std::mutex lock;
  23. };