`sync_bounded_queue::size` may return an incorrect value
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 58/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- cpp
調査の方向性
include/boost/thread/concurrent_queues/sync_bounded_queue.hpp から始め、full()、capacity()、および in_/out_ の状態と併せて size の実装を調べてください。issue にある wraparound のシーケンスを再現し、その後、push と pull のサイクルを対象とする回帰テストを追加または適応してください。wraparound 後に size() がキューに入っている要素数と一致すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug Report: sync_bounded_queue::size may return an incorrect value
Description
The sync_bounded_queue::size method can return an incorrect value under specific conditions.
Steps to Reproduce
- Create a
sync_bounded_queuewith a fixed capacity (e.g., 5). - Fill the queue to its maximum capacity using
try_push. - Empty the queue completely using
try_pull. - Push a single element into the queue.
After these steps, calling size() returns 0 instead of the expected 1. However, the empty() method correctly returns false.
Minimal Reproducible Example
#include <iostream>
#include <boost/thread/concurrent_queues/sync_bounded_queue.hpp>
int main() {
const size_t capacity = 5;
boost::sync_bounded_queue<int> queue(capacity);
size_t test_size = 0;
for (size_t i = 0; i < capacity; ++i) {
if (queue.try_push(i)) {
++test_size;
}
}
for (size_t i = 0; i < capacity; ++i) {
if (queue.try_pull()) {
--test_size;
}
}
if (queue.try_push(1)) {
++test_size;
}
std::cout << "size = " << queue.size() << "\ttest_size = " << test_size << std::endl;
std::cout << "empty = " << queue.empty() << "\ttest_empty = " << (test_size == 0) << std::endl;
std::cout << "[ TEST " << (queue.size() == test_size ? "PASSED" : "FAILED") << "]" << std::endl;
return 0;
}
Live Demo: https://godbolt.org/z/hMTvTT6Kq
Actual Output
size = 0 test_size = 1
empty = 0 test_empty = 0
[ TEST FAILED]
Expected Output
size = 1 test_size = 1
empty = 0 test_empty = 0
[ TEST PASSED]
Proposed Fix
The issue seems to be in the size method implementation. The current logic incorrectly handles the calculation when the queue has been wrapped around. The proposed fix simplifies the calculation:
--- a/include/boost/thread/concurrent_queues/sync_bounded_queue.hpp
+++ b/include/boost/thread/concurrent_queues/sync_bounded_queue.hpp
@@ -126,8 +126,7 @@ namespace concurrent
}
inline size_type size(lock_guard<mutex>& lk) const BOOST_NOEXCEPT
{
- if (full(lk)) return capacity(lk);
- return ((in_+capacity(lk)-out_) % capacity(lk));
+ return ((in_+capacity_-out_) % capacity_);
}
Validation
A comprehensive test has been created to verify the fix works correctly across multiple operations:
#include <iostream>
#include <boost/thread/concurrent_queues/sync_bounded_queue.hpp>
bool test_queue(boost::sync_bounded_queue<int>& queue, size_t& test_size, size_t count) {
for (size_t i = 0; i < 2 * queue.capacity(); ++i) {
for (size_t j = 0; j < count; ++j) {
++test_size;
if (!queue.try_push(0) || queue.size() != test_size) {
std::cout << "[ TEST FAILED ]" << std::endl;
return false;
}
}
for (size_t j = 0; j < count; ++j) {
--test_size;
if (!queue.try_pull() || queue.size() != test_size) {
std::cout << "[ TEST FAILED ]" << std::endl;
return false;
}
}
}
return true;
}
int main() {
const size_t capacity = 5;
boost::sync_bounded_queue<int> queue(capacity);
size_t test_size = 0;
for (size_t i = 1; i <= capacity; ++i) {
if (!test_queue(queue, test_size, i)) {
return 1;
}
}
return 0;
}
Test with current implementation (fails): https://godbolt.org/z/48nTe4zYP
Test with proposed fix (passes): https://godbolt.org/z/33Ysr9hsE
Environment
- Compiler: (any, reproduced on Godbolt with various compilers)
- Platform: (any)
- 主要言語
- C++
- スター
- 213
- フォーク
- 171
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
boostorg/thread のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 65/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 42/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
boostorg/thread の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
AXERA-TECH/ax-llm#77 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
games-on-whales/wolf#509 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
bug-unconfirmed
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
NVIDIA/cuda-samples#453 ·