lwm_std_mutex.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef BOOST_SMART_PTR_DETAIL_LWM_STD_MUTEX_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_LWM_STD_MUTEX_HPP_INCLUDED
  3. // Copyright 2020 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/assert.hpp>
  7. #include <mutex>
  8. namespace boost
  9. {
  10. namespace detail
  11. {
  12. class lightweight_mutex
  13. {
  14. private:
  15. std::mutex m_;
  16. lightweight_mutex(lightweight_mutex const &);
  17. lightweight_mutex & operator=(lightweight_mutex const &);
  18. public:
  19. lightweight_mutex()
  20. {
  21. }
  22. class scoped_lock;
  23. friend class scoped_lock;
  24. class scoped_lock
  25. {
  26. private:
  27. std::mutex & m_;
  28. scoped_lock(scoped_lock const &);
  29. scoped_lock & operator=(scoped_lock const &);
  30. public:
  31. scoped_lock( lightweight_mutex & m ): m_( m.m_ )
  32. {
  33. m_.lock();
  34. }
  35. ~scoped_lock()
  36. {
  37. m_.unlock();
  38. }
  39. };
  40. };
  41. } // namespace detail
  42. } // namespace boost
  43. #endif // #ifndef BOOST_SMART_PTR_DETAIL_LWM_STD_MUTEX_HPP_INCLUDED