@@ -48,7 +48,7 @@ class LeapArray {
4848 const int32_t bucket_length_ms_; // time length of each bucket
4949 private:
5050 const std::unique_ptr<WindowWrapSharedPtr<T>[]> array_;
51- std::mutex mtx_ ;
51+ mutable std::mutex leap_array_mtx_ ;
5252
5353 int32_t CalculateTimeIdx (/* @Valid*/ int64_t time_millis) const ;
5454 int64_t CalculateWindowStart (/* @Valid*/ int64_t time_millis) const ;
@@ -78,9 +78,11 @@ WindowWrapSharedPtr<T> LeapArray<T>::CurrentWindow(int64_t time_millis) {
7878 int64_t bucket_start = CalculateWindowStart (time_millis);
7979
8080 while (true ) {
81+ std::unique_lock<std::mutex> lck (leap_array_mtx_, std::defer_lock);
82+ leap_array_mtx_.lock ();
8183 WindowWrapSharedPtr<T> old = array_[idx];
84+ leap_array_mtx_.unlock ();
8285 if (old == nullptr ) {
83- std::unique_lock<std::mutex> lck (mtx_, std::defer_lock);
8486 if (lck.try_lock () && array_[idx] == nullptr ) {
8587 WindowWrapSharedPtr<T> bucket = std::make_shared<WindowWrap<T>>(
8688 bucket_length_ms_, bucket_start, NewEmptyBucket (time_millis));
@@ -90,7 +92,7 @@ WindowWrapSharedPtr<T> LeapArray<T>::CurrentWindow(int64_t time_millis) {
9092 } else if (bucket_start == old->BucketStart ()) {
9193 return old;
9294 } else if (bucket_start > old->BucketStart ()) {
93- std::unique_lock<std::mutex> lck (mtx_ , std::defer_lock);
95+ std::unique_lock<std::mutex> lck (leap_array_mtx_ , std::defer_lock);
9496 if (lck.try_lock ()) {
9597 ResetWindowTo (old, bucket_start);
9698 return old;
@@ -148,7 +150,9 @@ std::vector<WindowWrapSharedPtr<T>> LeapArray<T>::Buckets(
148150 }
149151 int size = sample_count_; // array_.size()
150152 for (int i = 0 ; i < size; i++) {
153+ leap_array_mtx_.lock ();
151154 auto w = array_[i];
155+ leap_array_mtx_.unlock ();
152156 if (w == nullptr || IsBucketDeprecated (time_millis, w)) {
153157 continue ;
154158 }
@@ -166,7 +170,9 @@ std::vector<std::shared_ptr<T>> LeapArray<T>::Values(
166170 }
167171 int size = sample_count_; // array_.size()
168172 for (int i = 0 ; i < size; i++) {
173+ leap_array_mtx_.lock ();
169174 WindowWrapSharedPtr<T> w = array_[i];
175+ leap_array_mtx_.unlock ();
170176 if (w == nullptr || IsBucketDeprecated (time_millis, w)) {
171177 continue ;
172178 }
0 commit comments