Advertisement
RobertDeMilo

BB3.7 еще раз о константности в многопоточной среде

Jun 13th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. //#include <iostream>
  2. //#include <map>
  3. //#include <string>
  4. //#include <vector>
  5. //#include <future>
  6. //#include <optional>
  7. //#include <algorithm>
  8. //#include <thread>
  9. //
  10. //using namespace std;
  11. //
  12. //template <typename T>
  13. //class LazyValue
  14. //{
  15. //public:
  16. //  explicit LazyValue(std::function<T()> init) : init_(std::move(init)) {}
  17. //
  18. //  const T& Get() const
  19. //  {
  20. //      if (lock_guard g(m); !value)
  21. //      {
  22. //          value = init_();
  23. //      }
  24. //      return *value;
  25. //  }
  26. //private:
  27. //  std::function<T()> init_;
  28. //  mutable std::optional<T> value;
  29. //  mutable mutex m;
  30. //};
  31. //
  32. //int main()
  33. //{
  34. //  LazyValue<map<string, int>> city_population([&]
  35. //      {
  36. //          return map<string, int> {
  37. //              {"Москва", 11514330},
  38. //              { "Санкт-Петербург", 1498921 }
  39. //          };
  40. //      });
  41. //
  42. //  auto kernel = [&]
  43. //      {
  44. //          return city_population.Get().at("Тула");
  45. //      };
  46. //
  47. //  vector<std::future<int>> ts;
  48. //
  49. //  for (size_t i = 0; i < 25; ++i)
  50. //  {
  51. //      ts.push_back(async(kernel));
  52. //  }
  53. //  for (auto& t : ts)
  54. //  {
  55. //      t.get();
  56. //  }
  57. //  const string saratov = "Саратов";
  58. //
  59. //  cout << saratov << ' ' << city_population.Get().at(saratov);
  60. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement