| 52 | |
| 53 | template <typename Keys> |
| 54 | std::vector<std::mutex *> MultiGet(const Keys &keys) { |
| 55 | std::set<unsigned, std::greater<unsigned>> to_acquire_indexes; |
| 56 | // We are using the `set` to avoid retrieving the mutex, as well as guarantee to retrieve |
| 57 | // the order of locks. |
| 58 | // |
| 59 | // For example, we need lock the key `A` and `B` and they have the same lock hash |
| 60 | // index, it will be deadlock if lock the same mutex twice. Besides, we also need |
| 61 | // to order the mutex before acquiring locks since different threads may acquire |
| 62 | // same keys with different order. |
| 63 | for (const auto &key : keys) { |
| 64 | to_acquire_indexes.insert(hash(key)); |
| 65 | } |
| 66 | |
| 67 | std::vector<std::mutex *> locks; |
| 68 | locks.reserve(to_acquire_indexes.size()); |
| 69 | for (auto index : to_acquire_indexes) { |
| 70 | locks.emplace_back(&mutex_pool_[index]); |
| 71 | } |
| 72 | return locks; |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | unsigned hash_power_; |