thread.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. \file thread.h
  3. \author Seung Geol Choi
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY 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 Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief Receiver Thread Implementation
  17. */
  18. #ifndef __THREAD_H__BY_SGCHOI
  19. #define __THREAD_H__BY_SGCHOI
  20. #include <condition_variable>
  21. #include <mutex>
  22. #include <thread>
  23. class CThread {
  24. public:
  25. CThread();
  26. virtual ~CThread();
  27. bool Start();
  28. bool Wait();
  29. bool IsRunning() const;
  30. protected:
  31. virtual void ThreadMain() = 0;
  32. bool m_bRunning;
  33. std::thread thread_;
  34. };
  35. class CLock {
  36. public:
  37. CLock() = default;
  38. ~CLock() = default;
  39. void Lock();
  40. void Unlock();
  41. // make CLock `BasicLockable`
  42. void lock();
  43. void unlock();
  44. private:
  45. std::mutex mutex_;
  46. };
  47. class CEvent {
  48. public:
  49. CEvent(bool bManualReset=false, bool bInitialSet=false);
  50. ~CEvent() = default;
  51. bool Set();
  52. bool Wait();
  53. bool IsSet() const;
  54. bool Reset();
  55. private:
  56. std::condition_variable cv_;
  57. mutable std::mutex mutex_;
  58. bool m_bManual;
  59. bool m_bSet;
  60. };
  61. #endif //__THREAD_H__BY_SGCHOI