You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
void push(const T& v) {
// Reserve the slot for the new object by advancing the producer count.
counts_t n = n_.load(std::memory_order_relaxed);
if (n.pc_ == cap_) abort();
int i = 0;
while (!n.equal(n_) || !n_.compare_exchange_weak(n, {n.pc_ + 1, n.cc_}, std::memory_order_acquire, std::memory_order_relaxed)) {
if (n.pc_ == cap_) abort();
nanosleep(i);
};
// Producer count advanced, slot n.pc_ + 1 is ours.
++n.pc_;
new (&s_[n.pc_]) T(v);
// Advance the consumer count to match.
if (!n_.compare_exchange_strong(n, {n.pc_, n.cc_ + 1}, std::memory_order_release, std::memory_order_relaxed)) abort();
}
std::optional<T> pop() {
// Decrement the consumer count.
counts_t n = n_.load(std::memory_order_relaxed);
if (n.cc_ == 0) return std::optional<T>(std::nullopt);
int i = 0;
while (!n.equal(n_) || !n_.compare_exchange_weak(n, {n.pc_, n.cc_ - 1}, std::memory_order_acquire, std::memory_order_relaxed)) {
if (n.cc_ == 0) return std::optional<T>(std::nullopt);
nanosleep(i);
};
// Consumer count decremented, slot n.cc_ - 1 is ours.
--n.cc_;
std::optional<T> res(std::move(s_[n.pc_]));
s_[n.pc_].~T();
// Decrement the producer count to match.
if (!n_.compare_exchange_strong(n, {n.pc_ - 1, n.cc_}, std::memory_order_release, std::memory_order_relaxed)) abort();
return res;
}
in
while (!n.equal(n_) || !n_.compare_exchange_weak(n, {n.pc_ + 1, n.cc_}, std::memory_order_acquire, std::memory_order_relaxed)) {
n.pc_ is already updated to n.pc_ + 1.
while (!n.equal(n_) || !n_.compare_exchange_weak(n, {n.pc_, n.cc_ - 1}, std::memory_order_acquire, std::memory_order_relaxed)) {
n.cc_ is already updated to n.cc_ -1.
why n.pc_ and n.cc_ updated again in the following:
++n.pc_;
--n.cc_;
are these update duplicate?
The text was updated successfully, but these errors were encountered:
For push and pop function.
in
while (!n.equal(n_) || !n_.compare_exchange_weak(n, {n.pc_ + 1, n.cc_}, std::memory_order_acquire, std::memory_order_relaxed)) {
n.pc_ is already updated to n.pc_ + 1.
while (!n.equal(n_) || !n_.compare_exchange_weak(n, {n.pc_, n.cc_ - 1}, std::memory_order_acquire, std::memory_order_relaxed)) {
n.cc_ is already updated to n.cc_ -1.
why n.pc_ and n.cc_ updated again in the following:
++n.pc_;
--n.cc_;
are these update duplicate?
The text was updated successfully, but these errors were encountered: