我正在阅读 boost::concurrent::sync_bounded_queue
的文档:
https://www.boost.org/doc/libs/1_72_0/doc/html/thread/sds.html
他们列出了一个部分:
Non-waiting Concurrent Queue Operations
还列出了一个部分:
Non-blocking Concurrent Queue Operations
我不明白非等待和非阻塞的区别?等待肯定是阻塞,它们是同一件事吗?
最佳答案
警告:以下所有内容均纯粹基于我在手册中阅读的内容。
该手册页提到非阻塞操作:
The interface is the same as the try operations but is allowed to also return queue_op_status::busy in case the operation is unable to complete without blocking.
在“非等待并发队列操作”部分下,列出了以下函数:
- s = q.try_push_back(e);
- s = q.try_push_back(rve);
- s = q.try_pull_front(lre);
因此,在我看来,只有当队列中没有更多位置时,非等待操作才会失败。如果另一个线程当前正在将数据推送到队列中,它们将等待其他推送完成,然后推送自己。仅当队列已满时它们才会失败。
而如果另一个线程正忙于将数据插入队列,则非阻塞操作将立即返回。即使队列中仍有足够的空间容纳新数据。
即如果非阻塞推送返回失败,并不一定意味着队列已满。
关于c++ - Boost并发队列非阻塞与非等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64610201/