关于boost::concurrent::sync_bounded_queue非等待与非阻塞操作的区别咨询
boost::concurrent::sync_bounded_queue Great question—this is a super common mix-up, since the terms feel interchangeable at first glance. Let’s unpack them clearly using the Boost docs you’re working through:
Non-waiting Concurrent Queue Operations
These operations have a simple rule: they never wait for a condition to be met before returning. They check the queue’s current state, perform the action only if it’s possible immediately, and send back a success/failure result right away—no hanging around, no waiting for space to free up or elements to arrive.
For example, Boost’s try_push() and try_pop() fit this category perfectly:
- Call
try_push()on a full bounded queue? It won’t wait for another thread to pop an element. It just returnsfalseinstantly. - Call
try_pop()on an empty queue? Same deal—it returnsfalseimmediately instead of waiting for an element to be added.
The core idea here is absolute immediacy: either the operation works right now, or it doesn’t. No waiting of any kind is involved.
Non-blocking Concurrent Queue Operations
This is a stricter, more formal concept from concurrent algorithm design. A non-blocking operation guarantees that the suspension of any single thread will never prevent other threads from making progress. These operations might use short, user-space loops (called spin waits) to retry the action briefly instead of putting the thread to sleep via the OS kernel, but they will never let the thread enter a kernel-blocked state (where the OS takes the thread off the CPU entirely).
In the context of boost::concurrent::sync_bounded_queue, non-blocking operations could include push/pop variants that avoid kernel-level blocking. Unlike non-waiting ops, they might retry the operation a few times in user space (without yielding the CPU to the OS) before giving up—but they’ll never get stuck in a kernel sleep that could delay other threads from doing their work.
Key Distinction at a Glance
- Non-waiting: "I’ll only do this if I can do it right now—no waiting, no retries, just immediate result."
- Non-blocking: "I won’t get stuck in a kernel-level block that blocks others, but I might try a few quick times before bailing."
To simplify further: All non-waiting operations are technically non-blocking (since they don’t wait or block), but not all non-blocking operations are non-waiting (some may spin briefly instead of returning instantly).
内容的提问来源于stack exchange,提问作者user997112

