From ab8da70abe85b4998bbc41c1f6107dc16f347014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Edelbo?= Date: Mon, 15 May 2023 17:09:36 +0200 Subject: [PATCH] Make CollectionParent::generate_key() threadsafe --- src/realm/collection_parent.cpp | 4 +++- src/realm/collection_parent.hpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/realm/collection_parent.cpp b/src/realm/collection_parent.cpp index d4c0f05ad30..270b8eb2164 100644 --- a/src/realm/collection_parent.cpp +++ b/src/realm/collection_parent.cpp @@ -262,11 +262,13 @@ CollectionBasePtr CollectionParent::get_collection_ptr(ColKey col_key) const } -int64_t CollectionParent::generate_key(size_t sz) const +int64_t CollectionParent::generate_key(size_t sz) { static std::mt19937 gen32; + static std::mutex mutex; int64_t key; + const std::lock_guard lock(mutex); do { if (sz < 0x10) { key = int8_t(gen32()); diff --git a/src/realm/collection_parent.hpp b/src/realm/collection_parent.hpp index 2fed4847fc2..7ac75585b6e 100644 --- a/src/realm/collection_parent.hpp +++ b/src/realm/collection_parent.hpp @@ -173,7 +173,7 @@ class CollectionParent : public std::enable_shared_from_this { SetBasePtr get_setbase_ptr(ColKey col_key) const; CollectionBasePtr get_collection_ptr(ColKey col_key) const; - int64_t generate_key(size_t sz) const; + static int64_t generate_key(size_t sz); }; } // namespace realm