- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下结构:
struct T {
~T() { delete[] buff; }
int* buff = nullptr; };
T* t = new T();
auto queue = boost::lockfree::queue<T*>(0);
// Thread A
t->buff = int[10];
queue.push(t);
// Thread Z
T* t = nullptr;
while(true)
if(queue.pop(t))
delete t; // Is this OK? If not, what kind of synchronization I need to make it OK?
一般问题是不同的线程(A 到 Y)在共享指针(而不是 std::shared_ptr)上一起工作。在某些时候,除了线程 A 之外没有人使用指针 t
线程 A 看到其他人对 *t
所做的一切,因此线程 A 可以安全地调用 delete t
。相反,它会插入 t
进入队列,线程 Z 从队列中弹出并执行 delete t
。问题是我们如何确保线程 Z 看到分配给 t->buff
的最后一个值? boost::lockfree::queue
有同步吗这保证了这一点?或者我们需要自己做一些事情(什么)?
最佳答案
是的,这是安全的!您对从队列中弹出的值拥有唯一的所有权。 push
必须使用 memory_order_release
(或更强),pop
必须使用 memory_order_acquire
(或更强),因为否则该实现将完全无用(您可以使用刚刚从队列中弹出的指针执行任何操作)。我没有检查代码,但你说实现使用了memory_order_seq_cst
,所以你应该很好!
关于c++ - boost::lockfree:queue 中的内存排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59586922/
我比较了这三种情况使用的运行时间,它们是boost::lockfree::queue, boost::lockfree::spsc_queue 以及使用std::queue的生产者/消费者队列的串行代
有人知道无锁常用数据类型的实现(即源代码)的好资源吗?我在考虑列表、队列等? 锁定实现非常容易找到,但我找不到无锁算法的示例,以及如何准确地执行 CAS 工作以及如何使用它来实现这些结构。 最佳答案
我无法强制销毁 pop 上的 boost::lockfree::spsc_queue 元素。 (当推送覆盖循环缓冲区的元素或列表被销毁时,即使通过引用访问元素,它们也会被正确销毁)。 我也无法直接访问
Boost documentation for spsc_queue说: read_available():线程安全且无需等待,只能从生产者线程调用 write_available():线程安全且无需
我想构建一个简单的 boost::lockfree::queue不接受任何参数且不返回任何值的函数。 看来 boost::lockfree::queue要求项目类型可以轻松分配和破坏,要求 boost
考虑以下结构: struct T { ~T() { delete[] buff; } int* buff = nullptr; }; T* t = new T(); auto queue = b
我正在尝试使用标记指针来处理列表上的无锁操作,以便在此事务期间如果其他线程在列表上操作时阻止比较和交换 (CAS) 通过。我的节点结构和 CAS 如下所示: struct node { uns
我们有一个类,我们将 boost::lockfree::queue 声明为 static 数据成员并覆盖 new 和 delete 运算符。 new/delete 运算符的逻辑是创建类的实例以获取/放
编辑:这不是重复的 任何允许在 post() 中锁定互斥锁的问题。请仔细阅读,我需要一个无锁的 post()!如果您没有真正的答案,请不要标记此重复项。 信号量(如在 linux 中)是一个有用的构建
boost::lockfree::queue 对要排队的数据结构有一些要求 普通复制构造函数 琐碎的作业 普通析构函数 请举个例子 最佳答案 明显的例子基本上是任何 POD 类: struct poi
我发现将 string 指针传递给 boost::lockfree::queue 会导致内存泄漏,因为 string指针无法完全释放。 boost::lockfree::stack 的情况是否相同?
我需要缓冲一堆传入的 10GigE 数据,以便稍后将其写入磁盘。按顺序执行此操作是一个问题,因为如果我不能足够快地维护它,我正在读取的设备将会溢出。 在寻找解决方案时,我偶然发现了 boost::lo
直到现在我还在我的项目中使用 std::queue。我测量了此队列上的特定操作所需的平均时间。 时间是在 2 台机器上测量的:我的本地 Ubuntu 虚拟机和远程服务器。使用 std::queue,两
常规 std::vector 有 emplace_back 避免不必要的复制。 spsc_queue 不支持这个有什么原因吗?是否由于某种原因无法使用无锁队列emplace? 最佳答案 我既不是 bo
我在看 Boost's lockfree queue . 当生产者线程将数据结构 T 插入缓冲区时,它会(通过复制构造函数)将数据复制到缓冲区中。 当消费者线程尝试调用consume_one() 来读
我有一个线程在 boost::lockfree::spsc_queue 上生成数据,以及一个线程消耗数据。我可以让第三个线程使用 read_available 监视队列吗?或 write_availa
考虑以下代码 struct foo { foo() { size_t someCalculatedValue = 2; bar.resize(someC
我正在使用 boost::lockfree::queue Foo(128)。 在弹出队列之前,我可以通过 Foo.empty() 函数检查队列的空状态。 我想知道我是否可以在推送之前以类似的方式检查其
所以我用boost::lockfree::queue写了一个多线程程序,代码和给定的例子差不多 http://www.boost.org/doc/libs/1_62_0/doc/html/lockfr
这里的问题是,boost::lockfree::queue 在分配内存后不会释放内存。为什么 freelist 节点没有返回给操作系统?调用单个集合对象的析构函数。 我想解决内存问题,关于无锁队列内存
我是一名优秀的程序员,十分优秀!