- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用无锁堆栈(通过标记指针)来管理一个小块内存池。当 block 插入池中和从池中删除时,列表节点就地创建和销毁。
这是一个非常简化的测试程序,它只从堆栈中弹出。因此,没有 ABA 问题,也没有标记指针。足以证明我正在参加的比赛:
#include <atomic>
#include <list>
#include <thread>
#include <type_traits>
struct Node {
Node() = default;
Node(Node *n) { next.store(n); }
std::atomic<Node *> next;
};
using Memory = std::aligned_storage_t<sizeof(Node)>;
struct Stack {
bool pop_and_use() {
for (Node *current_head = head.load(); current_head;) {
Node *next = current_head->next.load(); // READ RACE
if (head.compare_exchange_weak(current_head, next, std::memory_order_seq_cst)) {
current_head->~Node();
Memory *mem = reinterpret_cast<Memory *>(current_head);
new (mem) int{0}; // use memory with non-atomic write (WRITE RACE)
return true;
}
}
return false;
}
void populate(Memory *mem, int count) {
for (int i = 0; i < count; ++i) {
head = new (mem + i) Node(head.load());
}
}
std::atomic<Node *> head{};
};
int main() {
Memory storage[10000];
Stack test_list;
test_list.populate(storage, 10000);
std::thread worker([&test_list]() {
while (test_list.pop_and_use()) {
};
});
while (test_list.pop_and_use()) {};
worker.join();
return 0;
}
clang++-10 -fsanitize=thread tsan_test_2.cpp -o tsan_test_2 -O2 -g2 -Wall -Wextra && ./tsan_test_2
LLVMSymbolizer: error reading file: No such file or directory
==================
WARNING: ThreadSanitizer: data race (pid=35998)
Atomic read of size 8 at 0x7fff48bd57b0 by thread T1:
#0 __tsan_atomic64_load <null> (tsan_test_2+0x46d88e)
#1 std::__atomic_base<Node*>::load(std::memory_order) const /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/atomic_base.h:713:9 (tsan_test_2+0x4b3e6c)
#2 std::atomic<Node*>::load(std::memory_order) const /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/atomic:452:21 (tsan_test_2+0x4b3e6c)
#3 Stack::pop_and_use() /home/BOSDYN/akhripin/tmp/tsan_test_2.cpp:17:39 (tsan_test_2+0x4b3e6c)
#4 main::$_0::operator()() const /home/BOSDYN/akhripin/tmp/tsan_test_2.cpp:40:22 (tsan_test_2+0x4b3e6c)
#5 void std::__invoke_impl<void, main::$_0>(std::__invoke_other, main::$_0&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/invoke.h:60:14 (tsan_test_2+0x4b3e6c)
#6 std::__invoke_result<main::$_0>::type std::__invoke<main::$_0>(main::$_0&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/invoke.h:95:14 (tsan_test_2+0x4b3e6c)
#7 decltype(std::__invoke(_S_declval<0ul>())) std::thread::_Invoker<std::tuple<main::$_0> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/thread:244:13 (tsan_test_2+0x4b3e6c)
#8 std::thread::_Invoker<std::tuple<main::$_0> >::operator()() /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/thread:253:11 (tsan_test_2+0x4b3e6c)
#9 std::thread::_State_impl<std::thread::_Invoker<std::tuple<main::$_0> > >::_M_run() /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/thread:196:13 (tsan_test_2+0x4b3e6c)
#10 <null> <null> (libstdc++.so.6+0xbd6de)
Previous write of size 4 at 0x7fff48bd57b0 by main thread:
#0 Stack::pop_and_use() /home/BOSDYN/akhripin/tmp/tsan_test_2.cpp:21:9 (tsan_test_2+0x4b3d5d)
#1 main /home/BOSDYN/akhripin/tmp/tsan_test_2.cpp:43:20 (tsan_test_2+0x4b3d5d)
Location is stack of main thread.
Location is global '??' at 0x7fff48bad000 ([stack]+0x0000000287b0)
Thread T1 (tid=36000, running) created by main thread at:
#0 pthread_create <null> (tsan_test_2+0x4246bb)
#1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xbd994)
#2 __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310 (libc.so.6+0x21b96)
SUMMARY: ThreadSanitizer: data race (/home/BOSDYN/akhripin/tmp/tsan_test_2+0x46d88e) in __tsan_atomic64_load
==================
ThreadSanitizer: reported 1 warnings
current_head
的相同值时,就会出现问题。 , 但其中一个在另一个有机会读取
current_head->next
之前完成了弹出并覆盖了节点.
__tsan_acquire
和 __tsan_release
但找不到始终有效的东西。 inline Node *load_next_wrapper(Node *h) {
Node *ret;
asm volatile("movq (%1), %0" : "=r"(ret) : "r"(&h->next));
return ret;
}
最佳答案
如果您只是想重用数据结构中的相同节点,则标记指针很好,即您不破坏它,而只需将其放在空闲列表中,以便在下一个需要新节点时可以重用它推操作。在这种情况下,标记指针足以防止 ABA 问题,但它们无法解决您在这里面临的_内存回收问题_。
Another object of some type will be constructed in the same location. Eventually, it will be destroyed and the memory would return to the pool.
关于c++ - 如何避免或抑制此无锁堆栈中的竞争?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61919666/
我收到警告 Circular dependency detected! 在 Angular 6 中,我知道为什么会出现这个问题,但这根本不是问题。 我目前正在使用 SVG,按照我的逻辑,我会防止可能出
我试图抑制 clang/gcc 中地址清理程序发出的警告 我的源文件如下所示: int foo(){ double bar[] = {7,8}; return bar[3]; } int ma
我在微服务架构中有一个应用程序。该应用程序从不同的源获取数据,并且从其他各种应用程序获得许多不同的错误响应。某些异常(例如404-Not Found异常)可以引发并返回给最终用户,但其他异常(错误请求
我喜欢使用 readxl包裹。但是,我一直将它用作 RMarkdown 文档和一些 read_excel() 文档的一部分。读入文件时的消息在进行分析时很有用,但在尝试呈现分析时则不然。所以问题是,在
当我编译 JAXB 模式时,生成的类用通用 Javadoc 注释进行修饰。 是否有可能以某种方式抑制生成这些评论? 最佳答案 Annotation to disable JavaDocs Not if
我正在尝试运行专为 Vuze 设计的插件使用Java。为此,我使用这样的命令: /home/x/jre1.6.0_14/bin/java -Dazureus.config.path=/home/x/p
所以 - 我正在使用库 pyminizip - 这是我发现创建受密码保护的 zip 文件的唯一方法。当我使用它时,我收到一个弃用警告:“#”格式需要 PY_SSIZE_T_CLEAN。 现在,我无法控
这个问题在这里已经有了答案: Is ticklabel_format broken? (1 个回答) 关闭 2 年前。 我是 geopandas 的新手,我正在尝试用常规记数法而非科学记数法绘制带有
我从 Valgrind 得到以下日志: MPK ==5263== 4 bytes in 1 blocks are still reachable in loss record 1 of 84 ==52
有什么方法可以抑制 mediawiki 中的贬低警告吗?例如我在个人 wiki 站点的某些页面顶部收到此消息。 Deprecated: Use of MWNamespace::getSubject w
我正在使用 ScheduledThreadPoolExecutor 执行定期任务。 执行必须是周期性的,而不是固定的延迟。 我遇到了以下问题:考虑一个任务的时间为 1 分钟。如果任务需要 5 分钟才能
我是使用 RScaLAPACK 的 R 新手,每次我生成一个新的进程网格时,我都会收到一条消息。 > sla.gridInit(2) [1] "RScaLAPACK:Process Grid Init
我目前正在使用包 readr 读取文件.这个想法是使用 read_delim逐行读取以查找非结构化数据文件中的最大列数。代码输出有 parsing问题。我知道这些并将在导入后处理列类型。有没有办法关闭
所以我试图防止 fatal error 阻止我的脚本运行 所以我将错误报告设置为0: error_reporting(0); 然后我添加了一些垃圾代码.. junk code~~~~trolololo
在使用 summary() 函数(例如,stata 中的 absorb() 函数的等价物)时,是否有一种方法可以抑制线性模型中固定效应的系数。例如,我希望汇总函数只输出截距和 x,而不是因子的系数和标
如何抑制 SBT 的调试消息?它们被记录到标准输出,所以运行一个项目会产生这个: $ cat src/main/scala/Hello.scala object Hello { def main(
有没有办法抑制 Doxygen 对特定文件发出“未记录”警告?我的项目有几个自动生成的代码头,导致它抛出成百上千的错误,难以筛选。 最佳答案 如 documentation 中所述,有一个配置选项。
R的eHOF包中的HOF函数会自动生成进度条。函数中没有关闭进度条的参数。 有什么办法可以抑制函数外的进度条的生成? (我正在应用该功能数百次,目前进度条除了淹没我的屏幕外没有任何作用。) 最佳答案
随着所有 SDK 的出现,能够方便地针对多个 SDK 和平台进行构建。然而,从 3.2 跳到 3.0,甚至偶尔跳到 2.x,我经常收到涉及已更改或被取代的方法的弃用警告: warning: 'UIKe
我创建了一个 PowerShell 脚本,该脚本将为 Cisco Meraki 添加 VPN 连接。 脚本本身按预期运行,但如果发生错误,则会出现“已完成”弹出窗口,并在 PS 窗口中显示错误消息。
我是一名优秀的程序员,十分优秀!