gpt4 book ai didi

c++ - boost::lockfree::queue 在 c++11 中不是无锁的吗?

转载 作者:可可西里 更新时间:2023-11-01 16:47:58 25 4
gpt4 key购买 nike

在这个 websocket++ 示例中,我试图用 boost::lockfree::queue 替换 std::queue https://github.com/zaphoyd/websocketpp/blob/experimental/examples/broadcast_server/broadcast_server.cpp

看起来它可以在不真正改变任何语法但删除 boost::unique_lock 行的情况下完成。

但是,当我查看 boost 示例时,它有一个检查无锁的代码部分 http://boost-sandbox.sourceforge.net/doc/html/lockfree/examples.html

当我查看有关 lockfree::queue 的文档时,它在 is_lock_free() 上这样说 http://boost-sandbox.sourceforge.net/doc/html/boost/lockfree/queue.html :

bool is_lock_free(void) const;

Warning

It only checks, if the queue head and tail nodes and the freelist can be modified in a lock-free manner. On most platforms, the whole implementation is lock-free, if this is true. Using c++0x-style atomics, there is no possibility to provide a completely accurate implementation, because one would need to test every internal node, which is impossible if further nodes will be allocated from the operating system.

Returns: true, if implementation is lock-free.

我不知道“c++0x 风格的原子”是什么,但我很确定 c++0x 意味着 c++11。

我使用的是 c++11,只是将 boost::lockfree::queue 替换为 std::queue,所以这不会实现无锁吗?

最佳答案

没有。 “不可能提供完全准确的实现”评论指的是 is_lock_free() - 即不能保证 is_lock_free() 返回的结果准确反射(reflect)了实现是否是无锁的。但是,如果 is_lock_free() 返回 true,则该实现很可能是无锁的 - 但不是绝对的,铸铁保证。

关于c++ - boost::lockfree::queue 在 c++11 中不是无锁的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15326282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com