yield_k.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // boost/smart_ptr/detail/yield_k.hpp
  8. //
  9. // Copyright 2008, 2020 Peter Dimov
  10. //
  11. // inline void boost::detail::yield( unsigned k );
  12. //
  13. // Typical use:
  14. // for( unsigned k = 0; !try_lock(); ++k ) yield( k );
  15. //
  16. // Distributed under the Boost Software License, Version 1.0.
  17. // https://www.boost.org/LICENSE_1_0.txt
  18. #include <boost/smart_ptr/detail/sp_thread_pause.hpp>
  19. #include <boost/smart_ptr/detail/sp_thread_sleep.hpp>
  20. #include <boost/config.hpp>
  21. namespace boost
  22. {
  23. namespace detail
  24. {
  25. inline void yield( unsigned k )
  26. {
  27. // Experiments on Windows and Fedora 32 show that a single pause,
  28. // followed by an immediate sp_thread_sleep(), is best.
  29. if( k == 0 )
  30. {
  31. sp_thread_pause();
  32. }
  33. else
  34. {
  35. sp_thread_sleep();
  36. }
  37. }
  38. } // namespace detail
  39. } // namespace boost
  40. #endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED