diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp index 7ad4f4fd..b9e7d6d4 100644 --- a/td/telegram/Client.cpp +++ b/td/telegram/Client.cpp @@ -139,8 +139,13 @@ class MultiImpl { public: static std::shared_ptr get() { static std::mutex mutex; - static std::weak_ptr impl; + static std::vector > impls; std::unique_lock lock(mutex); + if (impls.size() == 0) { + impls.resize(clamp(thread::hardware_concurrency(), 8u, 1000u) * 5 / 4); + } + auto &impl = *std::min_element(impls.begin(), impls.end(), + [](auto &a, auto &b) { return a.lock().use_count() < b.lock().use_count(); }); auto res = impl.lock(); if (!res) { res = std::make_shared();