Skip to content

原子操作章节笔误 #37

@awei-mj

Description

@awei-mj

std::atomic<T*>小节

operator+=operator-=:以原子方式增加或减少指针的值。返回操作前的指针值

存在错误。以libstdc++为例,std::__atomic_base<_Tp*>的operator-=调用函数为sub_fetch而非fetch_sub:

__int_type
      operator-=(__int_type __i) volatile noexcept
      { return __atomic_sub_fetch(&_M_i, __i, int(memory_order_seq_cst)); }

故返回的是操作后的指针值,这也符合+=/-=的语义。在后续示例代码中代码正确,但注释也要相应修改:

// p 减 1,并返回原始值
x = (p -= 1);
assert(x == &array[1]);
assert(p.load() == &array[1]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions