Skip to content

Commit 9ce7064

Browse files
committed
Make DictionaryArray::dictionary() thread-safe
1 parent 97c656b commit 9ce7064

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

cpp/src/arrow/array/array_dict.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ DictionaryArray::DictionaryArray(const std::shared_ptr<DataType>& type,
108108
}
109109

110110
const std::shared_ptr<Array>& DictionaryArray::dictionary() const {
111-
if (!dictionary_) {
112-
// TODO(GH-36503) this isn't thread safe
111+
std::call_once(dictionary_init_flag_, [this]() {
113112
dictionary_ = MakeArray(data_->dictionary);
114-
}
113+
});
115114
return dictionary_;
116115
}
117116

cpp/src/arrow/array/array_dict.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <cstdint>
2121
#include <memory>
22+
#include <mutex>
2223

2324
#include "arrow/array/array_base.h"
2425
#include "arrow/array/data.h"
@@ -118,8 +119,10 @@ class ARROW_EXPORT DictionaryArray : public Array {
118119
const DictionaryType* dict_type_;
119120
std::shared_ptr<Array> indices_;
120121

121-
// Lazily initialized when invoking dictionary()
122+
// Lazily initialized when invoking dictionary().
123+
// Thread-safe initialization is ensured by dictionary_init_flag_.
122124
mutable std::shared_ptr<Array> dictionary_;
125+
mutable std::once_flag dictionary_init_flag_;
123126
};
124127

125128
/// \brief Helper class for incremental dictionary unification

0 commit comments

Comments
 (0)