Skip to content

Commit 15f2804

Browse files
committed
Fixing a subtle double-commit bug
1 parent ce469cd commit 15f2804

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

include/LockFreeSpscQueue.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ class LockFreeSpscQueue
172172
}
173173
}
174174

175+
/**
176+
* @brief Releases ownership of the transaction, preventing the destructor
177+
* from automatically committing the write.
178+
* @details This is an advanced feature used when another object, like a
179+
* WriteTransaction, needs to take over responsibility for the commit.
180+
*/
181+
void release() noexcept { m_owner_queue = nullptr; }
182+
175183
// This RAII object is move-only to ensure single ownership of a transaction.
176184
WriteScope(const WriteScope&) = delete;
177185
WriteScope& operator=(const WriteScope&) = delete;
@@ -628,7 +636,15 @@ class LockFreeSpscQueue
628636
if (scope.get_items_written() == 0) {
629637
return std::nullopt;
630638
}
631-
return WriteTransaction(this, scope.get_block1(), scope.get_block2());
639+
640+
// Create the transaction using the spans from the scope.
641+
auto transaction = WriteTransaction(this, scope.get_block1(), scope.get_block2());
642+
643+
// Release the scope so its destructor does nothing.
644+
// The WriteTransaction is now solely responsible for the final commit.
645+
scope.release();
646+
647+
return transaction;
632648
}
633649

634650
/** @brief Returns the total capacity of the queue (the size of the buffer). */

0 commit comments

Comments
 (0)