AtomicMinMax.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <algorithm>
  9. #include <atomic>
  10. // Implementations of atomic minimum and atomic maximum computations. These
  11. // are based on std::atomic_compare_exchange_strong.
  12. namespace WwiseGTE
  13. {
  14. template <typename T>
  15. T AtomicMin(std::atomic<T>& v0, T const& v1)
  16. {
  17. T vInitial, vMin;
  18. do
  19. {
  20. vInitial = v0;
  21. vMin = std::min(vInitial, v1);
  22. }
  23. while (!std::atomic_compare_exchange_strong(&v0, &vInitial, vMin));
  24. return vInitial;
  25. }
  26. template <typename T>
  27. T AtomicMax(std::atomic<T>& v0, T const& v1)
  28. {
  29. T vInitial, vMax;
  30. do
  31. {
  32. vInitial = v0;
  33. vMax = std::max(vInitial, v1);
  34. }
  35. while (!std::atomic_compare_exchange_strong(&v0, &vInitial, vMax));
  36. return vInitial;
  37. }
  38. }