@@ -463,6 +463,7 @@ template <class ElemTy> struct ConcurrentReadableArray {
463
463
auto *newStorage = allocate (newCapacity);
464
464
if (storage) {
465
465
std::copy (storage->data (), storage->data () + count, newStorage->data ());
466
+ newStorage->Count .store (count, std::memory_order_relaxed);
466
467
FreeList.push_back (storage);
467
468
}
468
469
@@ -471,11 +472,10 @@ template <class ElemTy> struct ConcurrentReadableArray {
471
472
Elements.store (storage, std::memory_order_release);
472
473
}
473
474
474
- auto Count = storage->Count .load (std::memory_order_relaxed);
475
- new (&storage->data ()[Count]) ElemTy (elem);
476
- storage->Count .store (Count + 1 , std::memory_order_release);
475
+ new (&storage->data ()[count]) ElemTy (elem);
476
+ storage->Count .store (count + 1 , std::memory_order_release);
477
477
478
- if (ReaderCount.load (std::memory_order_relaxed ) == 0 )
478
+ if (ReaderCount.load (std::memory_order_acquire ) == 0 )
479
479
for (Storage *storage : FreeList)
480
480
deallocate (storage);
481
481
}
@@ -485,14 +485,14 @@ template <class ElemTy> struct ConcurrentReadableArray {
485
485
// / count. This represents a snapshot of the contents at the time
486
486
// / `read` was called. The pointer becomes invalid after `f` returns.
487
487
template <class F > auto read (F f) -> decltype(f(nullptr , 0 )) {
488
- ReaderCount.fetch_add (1 , std::memory_order_relaxed );
488
+ ReaderCount.fetch_add (1 , std::memory_order_acquire );
489
489
auto *storage = Elements.load (std::memory_order_consume);
490
490
auto count = storage->Count .load (std::memory_order_acquire);
491
491
auto *ptr = storage->data ();
492
492
493
493
decltype (f (nullptr , 0 )) result = f (ptr, count);
494
494
495
- ReaderCount.fetch_sub (1 , std::memory_order_relaxed );
495
+ ReaderCount.fetch_sub (1 , std::memory_order_release );
496
496
497
497
return result;
498
498
}
0 commit comments